橘子味的心
标题:9.6 Google Map开发Android地图应用

下面通过一个实例完成一个简单的定位系统,并且在地图上显示当前的位置,实例的详细代码在 GPSLocationInMapDemo 项目中,实际调试时需要在真机上进行并且需要开启 GPS。运行效果如图 1 所示。

一个简单的定位系统
图 1  一个简单的定位系统
具体步骤如下:

1)创建一个新的工程并命名为 GPSLocationInMapDemo。

需要注意的是,由于要使用 Google Map API,因此创建的主 Activity 需要继承自 MapActivity,而不是 Activity。当选择 Build Target 时,应选择 Google APIs,如图 2 所示。

New Andriod Project对话框
图 2  New Andriod Project 对话框

2)在 AndroidManifest.xml 文件中的 <application> 标签中加入:

<uses-library android:name="com.google.android.maps" />

以便可以使用 Google Map API。为了使用 GPS 数据,在 <application> 标签之外加入如下权限:

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

为了从 Internet 获取地图数据,需要网络访问权限:

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

3)编写 main.xml 布局文件,具体代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical">
  7.  
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="string/hello" />
  12.  
  13. <TextView
  14. android:id="@+id/myLocationText"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"/>
  17.  
  18. <com.google.android.maps.MapView
  19. android:id="@+id/myMapView"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:apiKey="OrvIRrEPTuYsUXACd_p53h-ftI7T425PToOjKuQ"
  23. android:clickable="true" />
  24. </LinearLayout>
在 main.xml 布局中放置了两个 TextView 和一个 MapView 组件。

GPSLocationInMapDemoActivity.java 的代码如下:
  1. package introduction.android.gpsLocationInMapDemo;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationListener;
  6. import android.location.LocationManager;
  7. import android.os.Bundle;
  8. import android.widget.TextView;
  9.  
  10. import com.google.android.maps.GeoPoint;
  11. import com.google.android.maps.MapActivity;
  12. import com.google.android.maps.MapController;
  13. import com.google.android.maps.MapView;
  14. import com.google.android.maps.MyLocationOverlay;
  15.  
  16. public class GPSLocationInMapDemoActivity extends MapActivity {
  17. /**
  18. * Called when the activity is first created.
  19. */
  20. // 定义Location对象
  21. protected Location location;
  22. //定义MapView对象
  23. private MapView map;
  24. // 定义MyLocationOverlay 对象,在地图上标注当前位置
  25. private MyLocationOverlay myLocation;
  26. private MapController mapController;
  27. private TextView myLocationText;
  28. private GeoPoint geopoint;
  29. private double latitude;
  30. private double longitude;
  31.  
  32. /**
  33. * Called when the activity is first created.
  34. */
  35. protected boolean isRouteDisplayed() {
  36. return false;
  37. }
  38.  
  39. public void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.main);
  42. myLocationText = (TextView) findViewById(R.id.myLocation_Text);
  43. //定义 LocationManager对象
  44. LocationManager locationManager;
  45. String seviceName = Context.LOCATION_SERVICE;
  46. // 获取LocationManager对象
  47. locationManager = (LocationManager) getSystemService(seviceName);
  48. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
  49. // 得到MapView对象
  50. map = (MapView) findViewById(R.id.myMapView);
  51. //得到MapView对象的控制器
  52. mapController = map.getController();
  53. //设置 map 支持缩放工具条
  54. map.setBuiltInZoomControls(true);
  55. map.setSatellite(true);
  56. }
  57.  
  58. //得到LocationListener对象
  59. private final LocationListener locationListener = new LocationListener() {
  60. //当Provider处于不能使用时触发
  61. public void onProviderDisabled(String provider) {
  62.  
  63. }
  64.  
  65. //当状态发生改变时触发
  66. public void onStatusChanged(String provider, int status, Bundle extras) {
  67.  
  68. }
  69.  
  70. //当位置发生变化时触发
  71. @Override
  72. public void onLocationChanged(Location location) {
  73. // TODO Auto-generated method stub
  74. // 得到当前位置的纬度
  75. latitude = location.getLatitude();
  76. //得到当前时间的经度
  77. longitude = location.getLongitude();
  78. geopoint = new GeoPoint(new Double(latitude * 1E6).intValue(), new Double(longitude * 1E6).intValue());
  79. mapController.setCenter(geopoint);
  80.  
  81. //得到当前时间的MyLocationOverlay对象
  82. myLocation = new MyLocationOverlay(
  83. GPSLocationInMapDemoActivity.this, map);
  84. myLocation.enableMyLocation();
  85. // 将当前位置添加到地图上
  86. map.getOverlays().add(myLocation);
  87. //设置地图为卫星模式
  88. myLocationText.setText("当前位置纬度" + latitude + "\n当前位置经度"
  89. + longitude);
  90. }
  91.  
  92. //当Provider处于可用时触发
  93. @Override
  94. public void onProviderEnabled(String provider) {
  95. // TODO Auto-generated method stub
  96. }
  97. };
  98. }
MapView 组件通过网络载入所需地图,并且提供了拖曳地图的接口,用户可以直接在 MapView 中移动地图。MapView 提供了地图控制器,通过

mapController=map.getController();

方法可以获取到。通过控制器可以方便地控制 MapView 组件中地图的缩放和窗口移动。本实例中通过

mapController.setCenter(geopoint);

控制器将当前位置 geopoint 设置为 MapView 组件的中心。GeoPoint 代表的是地图上特定的点,需要注意的是,在 MapView 中使用的 geoPoint 的经纬度位置与从 GPS 中获取的经纬度存在一个 1E6 的比例差,需要经过转换后才能正确显示当前位置。本实例中的坐标转换代码为:

latitude = location.getLatitude();
longitude = location.getLongitude();
geopoint = new GeoPoint(new Double(latitude * 1E6).intValue(), new Double(longitude * 1E6).intValue());

MapView 提供了更加方便地对地图数据进行缩放的方式,通过

map.setBuiltInZoomControls(true);

在地图上放置一个缩放条(如图 3 所示),用户可以直接使用该缩放条对地图进行放大和缩小,而无须编写任何代码。
缩放条
图 3  缩放条

Overlay 是 Google Map API 提供的专门在地图上进行标记的类。本实例中使用 Overlay 标记当前的位置点。相关代码为:
  1. //得到当前时间的MyLocationOverlay对象
  2. myLocation = new MyLocationOverlay(GPSLocationInMapDemoActivity.this, map);
  3. myLocation.enableMyLocation();
  4. // 将当前位置添加到地图上
  5. map.getOverlays().add(myLocation);
至此,该实例开发完成。运行该实例需要支持 Google APIs 的 AVD,如图 4 所示。若没有,则可以通过 AVD Manager 创建一个。
 创建支持 Google APIs 的 AVD
图 4  创建支持 Google APIs 的 AVD