现在的位置: 首页 > 综合 > 正文

二十九、Google Map实现地图查询案例

2012年08月15日 ⁄ 综合 ⁄ 共 8573字 ⁄ 字号 评论关闭

一、        

Android google map的应用有:

1.我的位置:在地图上显示你的当前位置(通常在1000米范围内)。即使没有GPS,你也可以确定自己的位置。Google手机地图还支持内在GPS,也可以链接到蓝牙GPS传感器,以便更加准确定位用户的位置。“我的位置”的功能通过识别你附近无线发射塔的信息广播而确定你的位置的。

                 

2.地图和卫星视图:Google手机地图可以向你提供所查地区的地图和卫星视图,手机界面的使用感觉与你在台式机上相同。可沿着其中一个方向滚动,以查看地图上更多内容,或可以使用快捷键进行缩放。

             

3.商户列表:借助Google的本地搜索,可以按名称(如“星巴克”)或类型(如“咖啡”)——搜索商家,查看商店的营业时间和评分,然后,只需点击一下即可拨通感兴趣的商家的电话。有了“我的位置”功能,甚至都不用输入当前位置即可方便的找到附近的商家。

            

4.驾车路线:可以很方便的获得驾车路线,其中会很清楚地标识每次转弯。有了“我的位置”功能,甚至都不需要输入出发点。

                

5.公交换乘:查看公交和地铁线路,确定转车路线,制定你在全球80个城市的出行计划。

            

6.路况信息:Google地图上的公路会根据实时的路况数据,以绿色,黄色或红色显示。

                  

7.收藏夹:为你常去的地方加上书签,以便能在地图上非常方便地返回到这些地方。

         

二、在开发Google Map服务时,会使用到Google API中的com.google.android.map包,重要的几个类有:

1.MapActivity:这个类是用于显示Google Map的Activity类,它需要连接底层网络。MapActivity是一个抽象类,任何想要显示MapView的activity都需要派生自MapActivity,并且在其派生类的onCreate()中,都要创建一个MapView实例。

                 

2.MapView:MapView是用于显示地图的View组件。它派生自android.view.ViewGroup。它必须和MapActivity配合使用,而且只能被MapActivity创建,这是因为MapView需要通过后台的线程来连接网络或者文件系统,而这些线程需要有MapActivity来管理。

               

3.MapController:MapController用于控制地图的移动、缩放等。

              

4.OverLay:这是一个可显示于地图之上的可绘制的对象。

                 

5.GeoPoint:这是一个包含经纬度位置的对象。

       

三、实例开发

第一步、创建一个Android项目命令为mymap,注意模拟器请选择"Goolge APIs”,目录结构如下:

第二步、修改main.xml布局文件,修改内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content">
<!-- 经度 -->
<TextView android:text="@string/txtLongitude"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_vertical"/>
<EditText android:id="@+id/etLongitude"
android:text
="@string/etLongitude"
android:layout_width
="110px"
android:layout_height
="45px"/>
<!-- 纬度 -->
<TextView android:text="@string/txtLatitude"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center_vertical"
android:paddingLeft
="8px"/>
<EditText android:id="@+id/etLatitude"
android:text
="@string/etLatitude"
android:layout_width
="110px"
android:layout_height
="45px"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content">
<Button android:id="@+id/btnSearch"
android:text
="@string/btnSearch"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_weight
="4"/>
<RadioGroup android:id="@+id/rg"
android:orientation
="horizontal"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_weight
="1">
<!-- 普通视图 -->
<RadioButton android:text="@string/normal"
android:id
="@+id/normal"
android:checked
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content">
</RadioButton>
<!-- 卫星视图 -->
<RadioButton android:text="@string/satellite"
android:id
="@+id/satellite"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content">
</RadioButton>
</RadioGroup>
</LinearLayout>
<!-- 申请Android Google Map API key,
如有问题请查看 http://www.cnblogs.com/linjiqin/archive/2011/11/01/2232055.html
-->
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height
="fill_parent"
android:clickable
="true"
android:enabled
="true"
android:apiKey
="0ZUHwocAEeJEiMatLbTddLH_rS92w_CsVyGuNKQ"
android:id
="@+id/mapView"/>

</LinearLayout>

         

第三步、修改strings.xml配置文件,修改内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MapActivity!</string>
<string name="app_name">谷歌地图查询应用</string>
<string name="txtLongitude">经度:</string>
<string name="txtLatitude">纬度:</string>
<string name="btnSearch">查询</string>
<!-- 福州经纬度 -->
<string name="etLongitude">119.280</string>
<string name="etLatitude">26.080</string>
<string name="satellite">卫星视图</string>
<string name="normal">普通视图</string>
</resources>

           

第四步、修改AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="com.ljq.activity"
android:versionCode
="1"
android:versionName
="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GoogleMapActivity"
android:label
="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- 添加Google Map相关类库 -->
<uses-library android:name="com.google.android.maps"/>
</application>
<uses-sdk android:minSdkVersion="3"/>

<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

          

第五步、创建ArrowOverLay类,用来在地图中指定的经纬度位置绘制一个朝下的箭头,标明该位置在地图中的确切位置所在

package com.ljq.activity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
* 在地图中指定的经纬度位置绘制一个朝下的箭头,标明该位置在地图中的确切位置所在
*
*
@author Administrator
*
*/
public class ArrowOverLay extends Overlay{
private Bitmap bitmap;
private GeoPoint geoPoint;

public ArrowOverLay(Bitmap bitmap, GeoPoint geoPoint){
this.bitmap=bitmap;
this.geoPoint=geoPoint;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if(!shadow){
//获取Projection对象
Projection projection=mapView.getProjection();
Point point=new Point();
//将真实地理坐标转化为屏幕上的坐标
projection.toPixels(geoPoint, point);
//绘制箭头图片
canvas.drawBitmap(bitmap,
point.x-bitmap.getWidth()/2,
point.y-bitmap.getHeight(),
null);
}
}
}

            

第六步、创建GoogleMapActivity类,实现地图查询功能

package com.ljq.activity;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class GoogleMapActivity extends MapActivity {
private Bitmap bitmap;
private MapView mapView;
private MapController mapController;
// 普通视图
private RadioButton btnNormal;
// 卫星视图
private RadioButton btnSatellite;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
mapView = (MapView) findViewById(R.id.mapView);
// 设置是否显示放大缩小按钮
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
//设置倍数(1-21)
mapController.setZoom(15);
Button btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(onClickListener);
btnSearch.performClick();
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
btnNormal = (RadioButton) findViewById(R.id.normal);
btnSatellite = (RadioButton) findViewById(R.id.satellite);
rg.setOnCheckedChangeListener(onCheckedChangeListener);
}

/**
* 用来表示我们是否显示一些路线的信息,这个通常用在地图的飞行模式时才使用
*/
@Override
protected boolean isRouteDisplayed() {
return false;
}

View.OnClickListener onClickListener = new View.OnClickListener() {

public void onClick(View v) {
EditText etLongitude = (EditText) findViewById(R.id.etLongitude);
EditText etLatitude = (EditText) findViewById(R.id.etLatitude);
String longitude = etLongitude.getEditableText().toString().trim();
String latitude = etLatitude.getEditableText().toString().trim();
if (longitude.equals("") || latitude.equals("")) { // 判断是否输入空值
Toast.makeText(GoogleMapActivity.this, "对不起,请输入正确的经纬度坐标!", Toast.LENGTH_LONG).show();
return;
}
// 调用方法更新MapView
updateMapView(Double.parseDouble(latitude), Double.parseDouble(longitude));
}

};

OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

public void onCheckedChanged(RadioGroup group, int id) {
// 普通视图
if (id == btnNormal.getId()) {
// mapView.setTraffic(true); //设置为交通模式
// mapView.setSatellite(true); //设置为卫星模式
// mapView.setStreetView(true); //设置为街景模式
mapView.setTraffic(true);
mapView.setSatellite(false);
//卫星视图
} else if (id == btnSatellite.getId()) {
mapView.setTraffic(false);
mapView.setSatellite(true);
}
}

};

/**
* 更新MapView的视图
*
*
@param longitude
* 经度
*
@param latitude
* 纬度
*/
public void updateMapView(double latitude, double longitude) {
GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapView.displayZoomControls(true); // 设置显示放大缩小按钮
mapController.animateTo(geoPoint); // 将地图移动到指定的地理位置
List<Overlay> ols = mapView.getOverlays();
ols.clear();
ols.add(new ArrowOverLay(bitmap, geoPoint));// 添加一个新的Overlay
}
}

           

第七步、启动模拟器,运行结果如下:

普通视图:                                                                                 卫星视图:

          

 

 

抱歉!评论已关闭.