Location服务之LocationManager.docVIP

  • 3
  • 0
  • 约6.04千字
  • 约 7页
  • 2017-02-17 发布于北京
  • 举报
Location服务之LocationManager

上次介绍了位置服务中的Geocoder,这次就来介绍一下LocationManager。LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题,包括查询上一个已知位置、注册和注销来自某个LocationProvider的周期性的位置更新、注册和注销接近某个坐标时对一个已定义的Intent的触发等。今天我们就一起探讨一下LocationManager的简单应用。 在进入正题之前,朋友们需要了解与LocationManager相关的两个知识点: provider:LocationManager获取位置信息的途径,常用的有两种:GPS和NETWORK。GPS定位更精确,缺点是只能在户外使用,耗电严重,并且返回用户位置信息的速度远不能满足用户需求。NETWORK通过基站和Wi-Fi信号来获取位置信息,室内室外均可用,速度更快,耗电更少。为了获取用户位置信息,我们可以使用其中一个,也可以同时使用两个。 LocationListener:位置监听器接口,定义了常见的provider状态变化和位置的变化的方法,我们需要实现此接口,完成自己的处理逻辑,然后让LocationManager注册此监听器,完成对各种状态的监听。 既然上面讲到位置服务的核心是LocationManager,那么我们如何取得一个LocationManager呢?像其他系统服务一样,通过以下方式即可得到一个LocationManager实例: [java]?view plaincopy LocationManager?locMgr?=?(LocationManager)?getSystemService(Context.LOCATION_SERVICE);?? 对象实例是获取到了,可是怎么应用呢?下面就通过一个示例具体演示一下。 我们新建一个location项目。因为示例是基于地图服务的,所以创建时别忘了Build Target要选中Google APIs这一项。 然后修改/res/layout/main.xml,代码如下: [xhtml]?view plaincopy ?xml?version=1.0?encoding=utf-8??? FrameLayout?xmlns:android=/apk/res/android?? ????android:orientation=vertical?? ????android:layout_width=fill_parent?? ????android:layout_height=fill_parent?? ????com.google.android.maps.MapView?? ????????android:id=@+id/mapView?? ????????android:layout_width=fill_parent?? ????????android:layout_height=fill_parent?? ????????android:clickable=true?? ????????android:apiKey=your?apiKey?goes?here/?? ????Button?? ????????android:id=@+id/removeUpdates?? ????????android:layout_width=fill_parent?? ????????android:layout_height=wrap_content?? ????????android:text=removeUpdates/?? /FrameLayout?? 然后我们来看以下MainActivity.java文件,代码如下: [java]?view plaincopy package?com.scott.location;?? ?? import?android.content.Context;?? import?android.location.Location;?? import?android.location.LocationListener;?? import?android.location.LocationManager;?? import?android.os.Bundle;?? import?android.view.View;?? import?android.widget.Button;?? import?android.widget.ImageView;?? import?android.widget.Toast;?? ?? import?com.google.android.maps.GeoPoint;?? import?com.google.androi

文档评论(0)

1亿VIP精品文档

相关文档