【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.
//使用該方法的線程必須時開啓消息循環的線程,例如被調用activity的主線程(可以參考Android線程機制,默認新建的線程是沒有開啓消息循環的,主線程開啓消息循環,可以參考SDK的Looper類)
Parameters
provider     the name of the provider with which to register
minTime     the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance     the minimum distance interval for notifications, in meters
listener     a {#link LocationListener} whose onLocationChanged(Location) method will be called for each location update//這是事件監聽器

Throws
IllegalArgumentException     if provider or listener is null
RuntimeException     if the calling thread has no Looper  //運行時異常,在未開啓消息循環的線程中運行
SecurityException     if no suitable permission is present for the provider.

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

監聽器就不詳細的說了,那是java的基本知識,相信大家都很明白~

好啦,下面是我寫的一段代碼,通過GPS獲取地理位置信息並移動監聽位置變化


文章的鏈接 使用LOCATION獲取並且監聽位置變化的代碼





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章