Google Map Api 在android 中的使用

它包括如下功能:
·我的位置(测试版):“我的位置”在地图上显示你的当前位置(通常在 1000 米范围之内)。即使没有 GPS,你也可以确定自己的位置。谷歌手机地图还支持内置 GPS,也可以链接到蓝牙 GPS 传感器,以便更准确地确定用户的位置。“我的位置”功能是通过识别你附近无线发射塔的信息广播而确定你的位置的。
·地图和卫星视图:谷歌手机地图向你提供所查看地区的地图和卫星视图,其界面的使用感觉与你在台式机上相同。 可沿其中一个方向滚动,以查看地图上的更多内容;或使用快捷键进行缩放。
·商户列表:借助于 Google 的本地搜索引擎,可以按名称(如“星巴克”)或类型(如“咖啡”)搜索商家,查看商店的营业时间和评分,然后,只需点击一下即可拨通感兴趣的商家的电话。有了“我的位置”功能,甚至都不需要输入当前位置即可方便地找到附近的商家。
·驾车路线:可以很方便地获得驾车路线,其中会清楚地标明每次转弯。有了“我的位置”功能,甚至都不需要输入出发点。
·公交换乘:查看公交和地铁线路,确定转车路线,制定你在全球 80 多个城市的出行计划。“公交换乘”功能目前适用于黑莓、Windows Mobile、S60 和其他支持 Java 的手机。
·路况信息:Google 地图中的公路会根据实时路况数据,以绿色、黄色或红色显示。
·收藏夹:为你常去的地方加上书签,以便能在地图上非常方便地返回到这些地方。
大家不要认为这些功能在手机上很难实现,尤其是在我们要学习的Android平台中,要实现这些功能是非常简单的,只需要使用Android Maps API(地图API)和Android Location API(定位API)即可。下面我们将学习如何来使用这些API开发自己的地图应用。

在Android SDK?1.5预装的add-on中提供了一个Map扩展库com.google.android.maps,利用它就可以给android应用程序加上强大的地图功能了。这个库的位置是“Android SDK路径”\add-ons\google_apis-3\libs。需要说明的是,这个库并不是标准的Android sdk的内容,可以自己从这个位置下载,并放到你的sdk中,这样就可以为你新建的应用或者已有的应用加上地图功能了。在使用Android Map API之前,还需要申请一个Android Map API Key。

1.申请Android Map API Key

为了能顺利地申请Android Map API Key,必须要准备Google的账号和系统的证明书。一般Google发布Key都需要Google的账号,Google的账号是通用的,Gmail的账号就可以了(没有的话可以到http://www.google.com/去申请一个)。当一个应用程序发布时必须要证明书,证明书其实就是MD5。我们这里不是发布,而只是为了测试,可以使用Debug版的证明书。下面我们来学习如何申请Android Map API Key。

步骤1:找到你的debug.keystore文件。

证书的一般路径为:C:\Documents and Settings\当前用户\Local Settings\Application Data\ Android\debug.keystore。当然我们使用Eclipse开发,便可以打开Eclipse选择Windows→Preference→Android→Build,其中Default debug keystore的值便是debug.keystore的路径,如图9-8所示。

图9-8  debug.keystore文件的位置

步骤2:取得debug.keystore的MD5值。

首先在命令提示符下进入debug.keystore文件所在的路径,执行命令:keytool -list -keystore debug.keystore,这时可能会提示你输入密码,这里输入默认的密码“android”,即可取得MD5值,如图9-9所示。

图9-9  取得debug.keystore的MD5值

步骤3:申请Android Map的API Key。

打开浏览器,输入网址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登录Google账号,在Google的Android Map API Key申请页面上输入步骤2得到的MD5认证指纹,选中“I have read and agree with the terms and conditions”选项,如图9-10所示,按下“Generate API Key”按钮,即可得到我们申请到的API Key。

图9-10  Android Map API Key申请页面

到这里我们便完成了Android Map API Key的申请,记下申请的Android Map API Key值,在后面的应用程序中会用到它。下面我们还将创建一个基于Google APIs的AVD。

2.创建基于Google APIs的AVD

在Eclipse中打开AVD管理界面,在“Create AVD”部分的Name处填写AVD的名字,在Target处选择“Google APIs-1.5”,如图9-11所示,点击“Create AVD”按钮完成创建。

图9-11  创建AVD

3.创建基于Google APIs的工程

这里需要注意的是,由于我们需要使用Google APIs,所以在创建工程时,在Build Target处需要选择Google APIs,如图9-12所示,其他选项和以前一样。当然,在运行工程时也就需要选择我们刚刚创建的基于Google APIs的AVD来运行。



图9-12  创建基于Google APIs的工程

到这里,我们基本完成了准备工作,下面我们将开始学习如何使用Google API来开发地图应用程序。

Android中定义了一个名为com.google.android.maps的包,其中包含了一系列用于在Google Map上显示、控制和层叠信息的功能类,以下是该包中最重要的几个类:

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

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

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

·Overlay:这是一个可显示于地图之上的可绘制的对象。

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

下面我们将使用com.google.android.maps包来实现一个地图浏览程序(见本书所附代码:第9章\Examples_09_03)。

步骤1:创建工程,注意要选择的Build Target为“Google APIs”。

步骤2:修改AndroidManifest.xml文件。

由于我们要使用Google Map API,所以必须先在AndroidManifest.xml中定义如下信息:

1
[xml]  view plain copy
  1. <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><strong><uses-library</strong> <span style="color:rgb(0,0,102); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">android:name</span>=<span style="color:rgb(255,0,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">"com.google.android.maps"</span><strong>/></strong></span>  

,当然要从网络获取地图数据,还需要添加应用程序访问网络的权限。代码如下:

1
[xml]  view plain copy
  1. <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px"><strong><uses-library</strong> <span style="color:rgb(0,0,102); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">android:name</span>=<span style="color:rgb(255,0,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">"com.google.android.maps"</span> <strong>/></strong></span>  

步骤3:创建MapView。
要显示地图,需要创建一个MapView,在XML文件中的布局如代码清单9-3所示。其中android:apiKey的值便是我们申请的Android Map API Key。
代码清单9-3  main.xml


[html]  view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>     
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     
  3.  android:layout_width="fill_parent"     
  4.  android:layout_height="fill_parent"     
  5.  >     
  6. <com.google.android.maps.MapView   
  7. android:id="@+id/MapView01"   
  8. android:layout_width="fill_parent"   
  9. android:layout_height="fill_parent"   
  10. android:apiKey="0dYpmIXGIdwiVm-HEpzuUW2fjNYsFQ9EvYir1sg"/>   
  11. </RelativeLayout></span>  


当然,可以在程序中通过如下代码创建MapView。

1
[java]  view plain copy
  1. MapView map <span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">=</span> <span style="font-weight:bold; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">new</span> MapView<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="font-weight:bold; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">this</span>, <span style="color:rgb(0,0,255); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">"[Android Maps API Key]"</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">;</span>  

步骤4:实现MapActivity。
MapView需要由MapActivity来管理,所以程序部分应该继承自MapActivity类,必须实现isRouteDisplayed方法。MapView提供了3种模式的地图,分别可以通过以下方式设置采用什么模式来显示地图。另外,可以通过setBuiltInZoomControls方法设置地图是否支持缩放。


[java]  view plain copy
  1. <em>//设置为交通模式    
  2. //mMapView.setTraffic(true);    
  3. //设置为卫星模式    
  4. //mMapView.setSatellite(true);     
  5. //设置为街景模式    
  6. //mMapView.setStreetView(false);</em>  

步骤5:MapController的使用。
如果要设置地图显示的地点以及放大的倍数等,就需要使用MapController来控制地图。可以通过如下代码获得MapController对象:

mMapController= mMapView.getController();

要定位地点,需要构建一个GeoPoint来表示地点的经度和纬度,然后使用animateTo方法将地图定位到指定的GeoPoint上,代码如下:


[java]  view plain copy
  1. <em>//设置起点为成都  </em>  
  2. mGeoPoint<span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">=</span><strong>new</strong> GeoPoint<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><strong>int</strong><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">30.659259</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">*</span><span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">1000000</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span>,<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><strong>int</strong><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">104.065762</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">*</span><span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">1000000</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">;</span>    
  3. <em>//定位到成都  </em>  
  4. mMapController.<span style="color:rgb(0,102,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">animateTo</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span>mGeoPoint<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">;</span>  

步骤6:Overlay的使用。

如果需要在地图上标注一些图标文字等信息,就需要使用Overlay。这里我们首先要将地图上的经度和纬度转换成屏幕上实际的坐标,才能将信息绘制上去。Map API中提供了Projection.toPixels(GeoPoint in, Point out)方法,可以将经度和纬度转换成屏幕上的坐标。首先需要实现Overlay中的draw方法才能在地图上绘制信息,代码如下:

[java]  view plain copy
  1. <strong>class</strong> MyLocationOverlay <strong>extends</strong> Overlay  
  2.      <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">{</span>  
  3.       <strong>public boolean</strong> draw<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="color:rgb(0,51,153); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">Canvas</span> canvas,MapView mapView,<strong>boolean</strong> shadow,<strong>long</strong> when<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span>  
  4.       <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">{</span>  
  5.        <em>//...</em>  
  6.       <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">}</span>  
  7.      <span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">}</span>  

下面是示例运行效果,图9-13以交通模式显示地图,图9-14以卫星模式显示地图,它们都在屏幕上显示了一个图标,并标明了位置。

图9-13  交通模式地图

图9-14  卫星模式地图

该示例显示了成都市区的地图,标注了天府广场的大概位置,具体实现如代码清单9-4所示。

代码清单9-4  第9章\Examples_09_03\src\com\yarin\android\Examples_09_03\Activity01.java


[java]  view plain copy
  1. <span style="font-size:18px;">public class Activity01 extends MapActivity    
  2. {    
  3. private MapView mMapView;    
  4. private MapController mMapController;     
  5. private GeoPoint mGeoPoint;    
  6. public void onCreate(Bundle savedInstanceState)    
  7. {    
  8.     super.onCreate(savedInstanceState);    
  9.     setContentView(R.layout.main);    
  10.     mMapView = (MapView) findViewById(R.id.MapView01);    
  11.     //设置为交通模式    
  12.     //mMapView.setTraffic(true);    
  13.     //设置为卫星模式    
  14.     mMapView.setSatellite(true);     
  15.     //设置为街景模式    
  16.     //mMapView.setStreetView(false);    
  17.     //取得MapController对象(控制MapView)    
  18.     mMapController = mMapView.getController();     
  19.     mMapView.setEnabled(true);    
  20.     mMapView.setClickable(true);    
  21.     //设置地图支持缩放    
  22.     mMapView.setBuiltInZoomControls(true);     
  23.     //设置起点为成都    
  24.     mGeoPoint=new GeoPoint((int)(30.659259*1000000),(int)(104.065762*1000000));    
  25.     //定位到成都    
  26.     mMapController.animateTo(mGeoPoint);     
  27.     //设置倍数(1-21)    
  28.     mMapController.setZoom(12);     
  29.     //添加Overlay,用于显示标注信息    
  30.      MyLocationOverlay myLocationOverlay = new MyLocationOverlay();    
  31.      List<Overlay> list = mMapView.getOverlays();    
  32.      list.add(myLocationOverlay);    
  33. }    
  34. protected boolean isRouteDisplayed()    
  35. {    
  36.     return false;    
  37. }    
  38. class MyLocationOverlay extends Overlay    
  39. {    
  40.     public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)    
  41.     {    
  42.         super.draw(canvas, mapView, shadow);    
  43.         Paint paint = new Paint();    
  44.         Point myScreenCoords = new Point();    
  45.         // 将经纬度转换成实际屏幕坐标    
  46.         mapView.getProjection().toPixels(mGeoPoint,myScreenCoords);    
  47.         paint.setStrokeWidth(1);    
  48.         paint.setARGB(25525500);    
  49.         paint.setStyle(Paint.Style.STROKE);    
  50.         Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.home);    
  51.         canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);    
  52.         canvas.drawText("天府广场",myScreenCoords.x, myScreenCoords.y,paint);    
  53.         return true;    
  54.     }    
  55. }    
  56. }</span>  



定位系统

全球定位系统(Global Positioning System,GPS)又称为全球卫星定位系统,是一个中距离圆型轨道卫星导航系统,它可以为地球表面的绝大部分地区(98%)提供准确的定位、测速和高精度的时间标准。该系统由美国国防部研制和维护,可满足位于全球任何地方或近地空间的军事用户连续、精确地确定三维位置、三维运动和时间的需要。该系统包括太空中的24颗GPS卫星,地面上的1个主控站、3个数据注入站和5个监测站及作为用户端的GPS接收机。最少只需其中3颗卫星,就能迅速确定用户端在地球上所处的位置及海拔高度。所能连接到的卫星数越多,解码出来的位置就越精确。GPS广泛应用于军事、物流、地理、移动电话、数码相机、航空等领域,具有非常强大的功能,主要包括:

·精确定时:广泛应用在天文台、通信系统基站、电视台中。
·工程施工:道路、桥梁、隧道的施工中大量采用GPS设备进行工程测量。
·勘探测绘:野外勘探及城区规划中都有用到。
·导航。
·武器导航:精确制导导弹、巡航导弹。
·车辆导航:车辆调度、监控系统。
·船舶导航:远洋导航、港口/内河引水。
·飞机导航:航线导航、进场着陆控制。
·星际导航:卫星轨道定位。
·个人导航:个人旅游及野外探险。
·定位。
·车辆防盗系统。
·手机、PDA、PPC等通信移动设备防盗以及电子地图、定位系统。
·儿童及特殊人群的防走失系统。
·精准农业:农机具导航、自动驾驶以及土地高精度平整。

Android 支持地理定位服务的API。该地理定位服务可以用来获取当前设备的地理位置,应用程序可以定时请求更新设备当前的地理定位信息。比如应用程序可以借助一个Intent接收器来实现如下功能:以经纬度和半径划定一个区域,当设备出入该区域时,发出提醒信息,还可以和Google Map API一起使用,完成更多的任务。关于地理定位系统的API全部位于android.location包内,其中包括以下几个重要的功能类:

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

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

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

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

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

要使用地理定位,首先需要取得LocationManager的实例,在Android中,获得LocationManager的唯一方法是通过getSystemService()方法的调用。通过使用LocationManager,我们可以获得一个位置提供者的列表。在一个真实的手持设备中,这个列表包含了一些GPS服务。我们也可以选择更强大、更精确、不带有其他附加服务的GPS。代码如下:

1
[java]  view plain copy
  1. LocationManager<span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">?</span>locationManager<span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">?=?</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span>LocationManager<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">?</span>getSystemService<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span><span style="color:rgb(0,51,153); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">Context</span>.<span style="color:rgb(0,102,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">LOCATION_SERVICE</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">;</span>  

取得LocationManager对象之后,我们还需要注册一个周期性的更新视图,代码如下:

1
[java]  view plain copy
  1. locationManager.<span style="color:rgb(0,102,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">requestLocationUpdates</span><span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">(</span>LocationManager.<span style="color:rgb(0,102,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">GPS_PROVIDER</span>,<span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">1000</span>, <span style="color:rgb(204,102,204); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">0</span>,locationListener<span style="color:rgb(0,153,0); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">)</span><span style="color:rgb(51,153,51); padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px">;</span>  

其中第一个参数是设置服务提供者,第二个参数是周期,这里需要重点说明一下最后一个参数locationListener,它用来监听定位信息的改变,所以我们必须实现以下几个方法:

·onLocationChanged(Location location):当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发。

·onProviderDisabled(String provider):Provider禁用时触发此函数,比如GPS被关闭。

·onProviderEnabled(String provider):Provider启用时触发此函数,比如GPS被打开。

·onStatusChanged(String provider, int status, Bundle extras):Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数。

下面我们通过更改上一节的例子(本书所附代码:第9章\Examples_09_04)来实现自动通过定位系统获取用户当前的坐标,然后加载并显示地图,将坐标信息显示在一个TextView中,运行效果如图9-15所示。

图9-15 地图定位
要使用定位的API,首先需要在AndroidManifest.xml文件中添加其权限,具体代码如代码清单9-5所示。
代码清单9-5  第9章\Examples_09_04\AndroidManifest.xml


[html]  view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>   
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  3.        package="com.yarin.android.Examples_09_04"   
  4.        android:versionCode="1"   
  5.        android:versionName="1.0">   
  6.      <application android:icon="@drawable/icon" android:label="@string/app_name">   
  7.     <uses-library android:name="com.google.android.maps" />   
  8.          <activity android:name=".Activity01"   
  9.                    android:label="@string/app_name">   
  10.              <intent-filter>   
  11.                  <action android:name="android.intent.action.MAIN" />   
  12.                  <category android:name="android.intent.category.LAUNCHER" />   
  13.              </intent-filter>   
  14.          </activity>   
  15.      </application>   
  16.     <uses-permission android:name="android.permission.INTERNET"/>   
  17.       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>   
  18.       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>   
  19.      <uses-sdk android:minSdkVersion="5" />   
  20.  </manifest></span>  


由于我们在模拟器上测试,所以需要人为设置一个坐标。可以通过两种方法来设置一个模拟的坐标值。第一种方法是通过DDMS,我们可以在Eclipse的ADT插件中使用这种方法,只要启动Eclipse,选择“Window”->“Show View”,打开“Emulator Control”界面即可看到如下的设置窗口,我们可以手动或者通过KML和GPX文件来设置一个坐标。如图9-16所示。

图9-16  设置显示的坐标
另一种方法是使用geo命令,我们需要telnet到本机的5554端口,然后在命令行下输入类似于geo fix-121.45356 46.51119 4392 这样的命令,后面3个参数分别代表了经度、纬度和(可选的)海拔。设置之后在Android模拟器屏幕上便多出一个如图9-17所示的标志,表示模拟了GPS权限。

图9-17  GPS使用标志
现在我们可以使用位置管理器(LocationManager)和位置提供者进行getFromLocation的调用。这个方法返回本机当前位置的一个快照,这个快照将以 Location对象形式提供。在手持设备中,我们可以获得当前位置的经度和纬度;调用getFromLocationName方法可能返回一个数据,表示一个地方的名称。该例中我们还创建了一个菜单用来缩放地图,这时就使用地图控制器(MapController)的zoomIn和zoomOut方法来放大和缩小视图,具体实现如代码清单9-6所示。
代码清单9-6  第9章\Examples_09_04\src\com\yarin\android\Examples_09_04\Activity01.java

[java]  view plain copy
  1. <span style="font-size:18px;">public class Activity01 extends MapActivity     
  2. {    
  3.  public MapController mapController;    
  4.  public MyLocationOverlay myPosition;    
  5.  public MapView myMapView;    
  6.  private static final int ZOOM_IN=Menu.FIRST;     
  7.  private static final int ZOOM_OUT=Menu.FIRST+1;    
  8. public void onCreate(Bundle savedInstanceState) {    
  9.      super.onCreate(savedInstanceState);    
  10.      setContentView(R.layout.main);    
  11.      //取得LocationManager实例    
  12.      LocationManager locationManager;    
  13.      String context=Context.LOCATION_SERVICE;    
  14.      locationManager=(LocationManager)getSystemService(context);    
  15.      myMapView=(MapView)findViewById(R.id.MapView01);    
  16.      //取得MapController实例,控制地图    
  17.      mapController=myMapView.getController();    
  18.      //设置显示模式    
  19.      myMapView.setSatellite(true);    
  20.      myMapView.setStreetView(true);    
  21.      //设置缩放控制,这里我们自己实现缩放菜单    
  22.      myMapView.displayZoomControls(false);       
  23.      //设置使用MyLocationOverlay来绘图    
  24.      mapController.setZoom(17);    
  25.      myPosition=new MyLocationOverlay();    
  26.      List<Overlay> overlays=myMapView.getOverlays();    
  27.      overlays.add(myPosition);    
  28.      //设置Criteria(服务商)的信息    
  29.      Criteria criteria =new Criteria();    
  30.      //经度要求    
  31.      criteria.setAccuracy(Criteria.ACCURACY_FINE);    
  32.      criteria.setAltitudeRequired(false);    
  33.      criteria.setBearingRequired(false);    
  34.      criteria.setCostAllowed(false);    
  35.      criteria.setPowerRequirement(Criteria.POWER_LOW);    
  36.      //取得效果最好的criteria    
  37.      String provider=locationManager.getBestProvider(criteria, true);    
  38.      //得到坐标相关的信息    
  39.      Location location=locationManager.getLastKnownLocation(provider);    
  40.      //更新坐标    
  41.      updateWithNewLocation(location);    
  42.      //注册一个周期性的更新,3000ms更新一次    
  43.      //locationListener用来监听定位信息的改变    
  44.      locationManager.requestLocationUpdates(provider, 30000,locationListener);    
  45.  }    
  46.  private void updateWithNewLocation(Location location)     
  47.  {    
  48.      String latLongString;    
  49.      TextView myLocationText = (TextView)findViewById(R.id.TextView01);    
  50.      String addressString="没有找到地址\n";    
  51.      if(location!=null)    
  52.      {    
  53.          //为绘制标志的类设置坐标    
  54.          myPosition.setLocation(location);    
  55.          //取得经度和纬度    
  56.          Double geoLat=location.getLatitude()*1E6;    
  57.          Double geoLng=location.getLongitude()*1E6;    
  58.          //将其转换为int型    
  59.          GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());    
  60.          //定位到指定坐标    
  61.          mapController.animateTo(point);    
  62.          double lat=location.getLatitude();    
  63.          double lng=location.getLongitude();    
  64.          latLongString="经度:"+lat+"\n纬度:"+lng;    
  65.          double latitude=location.getLatitude();    
  66.          double longitude=location.getLongitude();    
  67.          //根据地理环境来确定编码    
  68.          Geocoder gc=new Geocoder(this,Locale.getDefault());    
  69.          try   
  70.          {    
  71.             //取得地址相关的一些信息、经度、纬度    
  72.              List<Address> addresses=gc.getFromLocation(latitude, longitude,1);    
  73.              StringBuilder sb=new StringBuilder();    
  74.              if(addresses.size()>0)    
  75.              {    
  76.                  Address address=addresses.get(0);    
  77.                  for(int i=0;i<address.getMaxAddressLineIndex();i++)    
  78.                      sb.append(address.getAddressLine(i)).append("\n");    
  79.                      sb.append(address.getLocality()).append("\n");    
  80.                      sb.append(address.getPostalCode()).append("\n");    
  81.                      sb.append(address.getCountryName());    
  82.                      addressString=sb.toString();    
  83.              }    
  84.          }catch(IOException e){}    
  85.      }    
  86.      else   
  87.      {    
  88.          latLongString="没有找到坐标.\n";    
  89.      }    
  90.      //显示    
  91.      myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);    
  92.  }    
  93.  private final LocationListener locationListener=new LocationListener()    
  94.  {    
  95.     //当坐标改变时触发此函数    
  96.      public void onLocationChanged(Location location)    
  97.      {    
  98.         updateWithNewLocation(location);    
  99.      }    
  100.      //Provider禁用时触发此函数,比如GPS被关闭     
  101.      public void onProviderDisabled(String provider)    
  102.      {    
  103.         updateWithNewLocation(null);    
  104.      }    
  105.      //Provider启用时触发此函数,比如GPS被打开    
  106.      public void onProviderEnabled(String provider){}    
  107.      //Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数    
  108.      public void onStatusChanged(String provider,int status,Bundle extras){}    
  109.  };    
  110.  protected boolean isRouteDisplayed()    
  111. {    
  112.     return false;    
  113. }    
  114.  //为应用程序添加菜单    
  115.  public boolean onCreateOptionsMenu(Menu menu)    
  116. {    
  117.     super.onCreateOptionsMenu(menu);    
  118.     menu.add(0, ZOOM_IN, Menu.NONE, "放大");    
  119.     menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");    
  120.     return true;    
  121. }    
  122.  public boolean onOptionsItemSelected(MenuItem item)    
  123. {    
  124.     super.onOptionsItemSelected(item);    
  125.     switch (item.getItemId())    
  126.     {    
  127.         case (ZOOM_IN):    
  128.             //放大    
  129.             mapController.zoomIn();    
  130.             return true;    
  131.         case (ZOOM_OUT):    
  132.             //缩小    
  133.             mapController.zoomOut();    
  134.             return true;    
  135.     }    
  136.     return true;    
  137. }    
  138. class MyLocationOverlay extends Overlay    
  139. {    
  140.     Location mLocation;    
  141.     //在更新坐标时,设置该坐标,以便画图    
  142.     public void setLocation(Location location)    
  143.     {    
  144.         mLocation = location;    
  145.     }    
  146.     @Override   
  147.     public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)    
  148.     {    
  149.         super.draw(canvas, mapView, shadow);    
  150.         Paint paint = new Paint();    
  151.         Point myScreenCoords = new Point();    
  152.         // 将经纬度转换成实际屏幕坐标    
  153.         GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.    
  154.         getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));    
  155.         mapView.getProjection().toPixels(tmpGeoPoint,myScreenCoords);    
  156.         paint.setStrokeWidth(1);    
  157.         paint.setARGB(25525500);    
  158.         paint.setStyle(Paint.Style.STROKE);    
  159.         Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);    
  160.         canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);    
  161.         canvas.drawText("Here am I",myScreenCoords.x,myScreenCoords.y, paint);    
  162.         return true;    
  163.     }    
  164. }    
  165. }</span>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值