Android定位和地图开发

Android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便。

             首先介绍一下地图包中的主要类:

             MapController :  主要控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view组件,管理Overlay,提供View的基本功能。使用多种地图模式(地图模式(某些城市可实时对交通状况进行更新),卫星模式,街景模式)来查看Google Map。常用方法:animateTo(GeoPoint point)  setCenter(GeoPoint point)  setZoom(int zoomLevel) 等。

             Mapview  : 是用来显示地图的view, 它派生自android.view.ViewGroup。当MapView获得焦点,可以控制地图的移动和缩放。地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)  setTraffic(boolean), setStreetView(boolean) 方法。

           Overlay   : 是覆盖到MapView的最上层,可以扩展其ondraw接口,自定义在MapView中显示一些自己的东西。MapView通过MapView.getOverlays()对Overlay进行管理。

          Projection :MapView中GPS坐标与设备坐标的转换(GeoPoint和Point)。

          定位系统包中的主要类:

         LocationManager:本类提供访问定位服务的功能,也提供获取最佳定位提供者的功能。另外,临近警报功能也可以借助该类来实现。

        LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。

        LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。

        Criteria:该类使得应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者。

        Geocoder:用于处理地理编码和反向地理编码的类。地理编码是指将地址或其他描述转变为经度和纬度,反向地理编码则是将经度和纬度转变为地址或描述语言,其中包含了两个构造函数,需要传入经度和纬度的坐标。getFromLocation方法可以得到一组关于地址的数组。


         下面开始地图定位实例的开发,在开发地图前需要 获取Android 地图 API 密钥  网上有很多资料,这里就不再复述。

         首先要在manifest.xml中设置全相应的权限和maps库:


  1. <application
  2. android:icon="@drawable/ic_launcher"
  3. android:label="@string/app_name" >
  4. <activity
  5. android:label="@string/app_name"
  6. android:name=".MyMapActivity" >
  7. <intent-filter >
  8. <action android:name="android.intent.action.MAIN" />

  9. <category android:name="android.intent.category.LAUNCHER" />
  10. </intent-filter>
  11. </activity>
  12. <span style="color:#FF6666;">
  13. <uses-library android:name="com.google.android.maps" /></span>
  14. </application>

  15. <span style="color:#FF6666;"> <uses-permission android:name="android.permission.INTERNET" />
  16. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  17. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /></span>
复制代码
在上面我标红的千万不要忘记。
      layout下的main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >

  6. <com.google.android.maps.MapView
  7. android:id="@+id/mapview"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:apiKey="008uu0x2a7GWlK2LzCW872afBAPLhJ-U2R26Wgw"
  11. />

  12. </LinearLayout>
复制代码
下面是核心代码,重要的地方我做了注释:

  1. public class MyMapActivity extends MapActivity {
  2. /** Called when the activity is first created. */
  3. private MapController mapController;
  4. private MapView mapView;
  5. private MyOverLay myOverLay;

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

  10. LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
  11. mapView=(MapView) this.findViewById(R.id.mapview);
  12. //设置交通模式
  13. mapView.setTraffic(true);
  14. //设置卫星模式
  15. mapView.setSatellite(false);
  16. //设置街景模式
  17. mapView.setStreetView(false);
  18. //设置缩放控制
  19. mapView.setBuiltInZoomControls(true);
  20. mapView.setClickable(true);
  21. mapView.setEnabled(true);
  22. //得到MapController实例 
  23. mapController=mapView.getController();
  24. mapController.setZoom(15);

  25. myOverLay=new MyOverLay();
  26. List<Overlay> overLays=mapView.getOverlays();
  27. overLays.add(myOverLay);

  28. Criteria criteria=new Criteria();
  29. criteria.setAccuracy(Criteria.ACCURACY_FINE);
  30. criteria.setAltitudeRequired(false);
  31. criteria.setBearingRequired(false);
  32. criteria.setCostAllowed(false);
  33. criteria.setPowerRequirement(Criteria.POWER_LOW);
  34. //取得效果最好的Criteria
  35. String provider=locationManager.getBestProvider(criteria, true);
  36. //得到Location
  37. Location location=locationManager.getLastKnownLocation(provider);
  38. updateWithLocation(location);
  39. //注册一个周期性的更新,3秒一次
  40. locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);

  41. }
  42. @Override
  43. public boolean onCreateOptionsMenu(Menu menu) {
  44. // TODO Auto-generated method stub
  45. menu.add(0, 1, 1, "交通模式");
  46. menu.add(0,2,2,"卫星模式");
  47. menu.add(0,3,3,"街景模式");

  48. return super.onCreateOptionsMenu(menu);
  49. }
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // TODO Auto-generated method stub
  53. super.onOptionsItemSelected(item);
  54. switch (item.getItemId()) {
  55. case 1://交通模式
  56. mapView.setTraffic(true);
  57. mapView.setSatellite(false);
  58. mapView.setStreetView(false);
  59. break;
  60. case 2://卫星模式
  61. mapView.setSatellite(true);
  62. mapView.setStreetView(false);
  63. mapView.setTraffic(false);
  64. break;
  65. case 3://街景模式
  66. mapView.setStreetView(true);
  67. mapView.setTraffic(false);
  68. mapView.setSatellite(false);
  69. break;
  70. default:
  71. mapView.setTraffic(true);
  72. mapView.setSatellite(false);
  73. mapView.setStreetView(false);
  74. break;
  75. }
  76. return true;
  77. }
  78. private void updateWithLocation(Location location){
  79. if(location!=null){
  80. //为绘制类设置坐标
  81. myOverLay.setLocation(location);
  82. GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
  83. //定位到指定的坐标
  84. mapController.animateTo(geoPoint);
  85. mapController.setZoom(15);
  86. }
  87. }
  88. private final LocationListener locationListener=new LocationListener() {

  89. @Override
  90. public void onStatusChanged(String provider, int status, Bundle extras) {
  91. // TODO Auto-generated method stub

  92. }

  93. @Override
  94. public void onProviderEnabled(String provider) {
  95. // TODO Auto-generated method stub

  96. }

  97. @Override
  98. public void onProviderDisabled(String provider) {
  99. // TODO Auto-generated method stub

  100. }
  101. //当坐标改变时出发此函数
  102. @Override
  103. public void onLocationChanged(Location location) {
  104. // TODO Auto-generated method stub
  105. updateWithLocation(location);
  106. }
  107. };
  108. class MyOverLay extends Overlay{

  109. private Location location;
  110. public void setLocation(Location location){
  111. this.location=location;
  112. }

  113. @Override
  114. public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
  115. long when) {
  116. // TODO Auto-generated method stub
  117. super.draw(canvas, mapView, shadow);
  118. Paint paint=new Paint();
  119. Point myScreen=new Point();
  120. //将经纬度换成实际屏幕的坐标。
  121. GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
  122. mapView.getProjection().toPixels(geoPoint, myScreen);
  123. paint.setStrokeWidth(1);
  124. paint.setARGB(255, 255, 0, 0);
  125. paint.setStyle(Paint.Style.STROKE);
  126. Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.mypicture);
  127. //把这张图片画到相应的位置。
  128. canvas.drawBitmap(bmp, myScreen.x, myScreen.y,paint);
  129. canvas.drawText("天堂没有路", myScreen.x, myScreen.y, paint);
  130. return true;

  131. }
  132. }
  133. @Override
  134. protected boolean isRouteDisplayed() {
  135. // TODO Auto-generated method stub
  136. return false;
  137. }
  138. @Override
  139. public boolean onKeyDown(int keyCode, KeyEvent event) {
  140. // TODO Auto-generated method stub

  141. if (keyCode == KeyEvent.KEYCODE_BACK) {
  142. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  143. builder.setMessage("你确定退出吗?")
  144. .setCancelable(false)
  145. .setPositiveButton("确定",
  146. new DialogInterface.OnClickListener() {
  147. public void onClick(DialogInterface dialog,
  148. int id) {
  149. MyMapActivity.this.finish();
  150. android.os.Process
  151. .killProcess(android.os.Process
  152. .myPid());
  153. android.os.Process.killProcess(android.os.Process.myTid());
  154. android.os.Process.killProcess(android.os.Process.myUid());
  155. }
  156. })
  157. .setNegativeButton("返回",
  158. new DialogInterface.OnClickListener() {
  159. public void onClick(DialogInterface dialog,
  160. int id) {
  161. dialog.cancel();
  162. }
  163. });
  164. AlertDialog alert = builder.create();
  165. alert.show();
  166. return true;
  167. }

  168. return super.onKeyDown(keyCode, event);
  169. }
  170. }
复制代码
接下来看一下运行后效果:



       0_1328753403tOT6.gif

   

可以放大缩小:


    0_1328753416gvUt.gif

   

可是使用menu键,切换不同的模式:


    0_1328753569NsOt.gif

   


上面是切换到了卫星模式。由于地图需要耗费大量的网络资源,如果网络比较慢的话会等待很长时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值