ITEEDU

定位服务API

Android的SDK关于定位服务提供了两个包:android.location 和 com.google.android.maps。请阅读下面的内容对这两个包有个初步的了解。

android.location

这个包包含了Android平台下面关于定位服务的几个类。最重要的是,它引入了LocationManager这种服务,该服务有提供了定位(如果硬件支持定位)的API。LocationManager不应该直接实例化,而是通过getSystemService(Context.LOCATION_SERVICE)返回一个它的句柄。

一旦你的程序拥有了LocationManager句柄,你可以做下面三件事情:

  • 获得LocationManager知道的地点提供者的列表,并且获得LocationManager最后的已知地点。Query for the list of all LocationProviders known to the LocationManager for its last known location.
  • 向LocationProvider注册/注销一个当前地点的更新。
  • 如果设备有一个大概半径的范围,注册/注销一个给定的方向。

即使是这样,在最初的开发中,你可能还不能从地点提供商((Network or GPS)那获得任何数据。因此,在编程虚拟一些地点数据对你的编程很有必要。

注意: 如果你是使用早期SDK(m3/m5)中的虚拟地点提供商,你就不能在/system/etc/location文件夹下进行了。这个目录在boot-up的时候会被删除,请按照下面的步骤。

提供虚拟地址数据

当你在Android下面测试你的程序时,你有两个方法向你的程序发送虚拟定位数据:使用DDMS工具或使用“geo”命令。

使用 DDMS

With the DDMS tool, you can simulate location data a few different ways:

  • Manually send individual longitude/latitude coordinates to the device.
  • Use a GPX file describing a route for playback to the device.
  • Use a KML file describing individual placemarks for sequenced playback to the device.

For more information on using DDMS to spoof location data, see the Using DDMS guide.

Using the "geo" command

Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory. Now you can use:

  • geo fix to send a fixed geo-location.

    This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:

    geo fix -121.45356 46.51119 4392
  • geo nmea to send an NMEA 0183 sentence.

    This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). For example:

    geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62

com.google.android.maps

This package introduces a number of classes related to rendering, controlling, and overlaying customized information on your own Google Mapified Activity. The most important of which is the MapView class, which automagically draws you a basic Google Map when you add a MapView to your layout. Note that, if you want to do so, then your Activity that handles the MapView must extend MapActivity.

Also note that you must obtain a MapView API Key from the Google Maps service, before your MapView can load maps data. For more information, see Obtaining a MapView API Key.

Once you've created a MapView, you'll probably want to use getController() to retrieve a MapController, for controlling and animating the map, and ItemizedOverlay to draw Overlays and other information on the Map.

This is not a standard package in the Android library. In order to use it, you must add the following node to your Android Manifest file, as a child of the <application> element:

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