当前位置:操作系统 > 安卓/Android >>

Android笔记 二 Location获取地理位置信息(上)

 

2011 7 22暑假实训的第五天,跟大家分享以下我对Android location的学习吧,这是一个最基本的获取地理位置信息的入门,下一次给大家介绍更质能化的地理位置选择Criteria,今天就先写简单的吧,挑选了SDK两个重要的方法,我自己翻译了一下,也加上了自己的理解,希望大家指教,开始吧!获取跟踪你的位置~~~

提供重要的地理位置信息服务

1 Location Manager 管理服务

2 Location Provider 提供数据的content provider

 

方式一:GPS 特点:精度高,耗电量大,不耗费流量 权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

方式二:NETWORK 特点:精度低,省电,需要网络访问  权限<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

                  或者权限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

方式三PASSIVE_PROVIDER 资料比较少,只用于特定的情景下,SDK的解释是,并不自己实例化去获取地理位置,而是通过getProvider获取其他的服务或者activity更新位置,被动的获取更新。

 

 

操作基本步骤:

1 在manifest.xml文件中设置权限

2 获取LocaionManager

3 选择provider

4 创建listener

 

 

/**********************************************************************************************************************/

第一部分 权限

android的安全服务机制,如果应用要访问本地的资源例如联系人列表、拨号、GPS或者其他应用程序的数据,需要许可。所以要使用地理位置信息的服务需要在<manifest>标签下添加android:name="android.permission.ACCESS_FINE_LOCATION"/>获得许可

 

也可通过单击manifest.xml文件的permission标签可视化的添加许可

 

/**********************************************************************************************************************/

第二部分  认识LocationManager

 

官方SDK解释“This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE). ”

翻译一下大概意思是“LocationManager这个类提供了对系统位置服务的访问,这些服务允许应用程序获取设备地理位置的定期的更新,也可以在设备接近一个指定的地理位置的时候发起一个指定activity的intent。你不需要创建LocationManager的实例,取而代之的是通过Context.getSystemService(Context.LOCATION_SERVICE)获取。”

 

重要的方法:来自官方SDK(附上我的翻译注释)

 

-----------------------------------------------------------------------------------------------------------------------------------------------

public Location getLastKnownLocation (String provider)

Since: API Level 1

 

Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

 

//返回一个Location,这个location标明从给定provider获得最后已知的位置,也就是最近获取的位置。这个操作可以不必要启动provider,注意这个地址可能是已经过期的,例如使用的设备可能已经被关闭或者转向了另一个位置。

 

If the provider is currently disabled, null is returned.

//如果当前的provider被禁用,函数返回null

 

Parameters

provider     the name of the provider

Returns

 

    the last known location for the provider, or null

 

Throws

SecurityException     if no suitable permission is present for the provider.  //前面提到的权限的问题,没有允许会抛出安全性的异常,android系统的机制

IllegalArgumentException     if provider is null or doesn't exist            //非法参数异常,表示provider为null,或者不存在

------------------------------------------------------------------------------------------------------------------------------------------------

一些补充:

认识Location类

官方SDK解释 :“A class representing a geographic location sensed at a particular time (a "fix"). A location consists of a latitude and longitude, a UTC timestamp. and optionally information on altitude, speed, and bearing.

大致的意思是:“这个类用来表示在一个特定时间被感应的地理位置信息(我们叫一个fix//感觉有点聚焦的意思,呵呵),一个location包括一个经度,纬度,一个世界时间戳还有一些关于海拔,速度和方向的可选信息。 “

可以通过getLatitude() getLongitude() getProvider()等函数来获取封装信息中的经度纬度和提供信息的provider,这个比较简单。

 

 

-----------------------------------------------------------------------------------------------------------------------------------------------

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

Since: API Level 1

 

Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

//注册当前的activity定时的被provider的被通知。相当于注册事件。每隔一段时间,LocationListenner会被调用,同时当前的位置或者状态进行更新。

 

It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.

//接受最新的位置信息可能会花费一会时间,如果需要立刻获得位置信息,程序可以使用上面讲的getLastKonwnLocation方法

 

In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.

 

//假如provider被用户停止,(比如关闭GPS等),更新会停止。并且onProviderDisabled方法(监听器中一个需要复写的方法)会被调用,只要provider再次变为可用状态,onProviderEnable方法会被调用,并且更新操作立刻开始。

 

The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

//通知(更新)的频率可以通过使用minTime(最小更新时间 单位:毫秒)和minDistance(单位:米)参数,如果minTime大于0,LocationManager能够在minTime时间内休息来保存电量,如果minDistance大于0,每变化这个距离就会进行一次更新,如果希望尽可能频繁的更新的数据,则把两个参数均设置为0.

 

Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.

//后台的服务应该注意,设置一个合理的minTime使得设备一直保持GPS或者WIFI的时候不耗费太多的电量。不推荐使用6000ms的minTime值

 

The calling thread must be a Looper thread such as the main thread of the calling Activity.<

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,