原文地址:http://www.devdiv.com/Android-android_-thread-129718-1-1.html
一、LocationManager類簡介
Android中LocationManager類提供了獲取系統地理位置服務,這些服務允許應用程序定期獲取設備地理位置更新信息,或者查詢上一個已知位置;註冊/註銷來自某個LocationProvider的週期性的位置更新;以及註冊/註銷接近某個座標時對一個已定義Intent的觸發等。
以下是對android location包中一些重要的類的說明:
LocationProvider:是location提供者類的抽象父類。一個Location提供者提供了設備定期的地理位置信息。
LocationListener:當地理位置改變時,該類提供了回調函數通知應用程序。這個監聽對象必須被LocationManager註冊。
Criteria:該類爲應用程序選擇合適的Location Provider。
下面簡要描述一下LocationManager的工作流程:
首先,直接實例化LocationManager是不允許的,必須通過以下代碼來獲取LocationManager實例。
1 |
LocationManager
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); |
通過得到的LocationManager實例,我們可以通過下面的語句來註冊一個週期性的位置更新。以下代碼從GPS獲取位置信息,並且每隔5秒更新一次。最後一個參數是LocationListener的一個引用,我們必須要實現這個類。
01 |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000 , 0 ,
locationListener); |
03 |
private final LocationListener
locationListener = new LocationListener()
{ |
04 |
public void onLocationChanged(Location
location) { |
06 |
if (location
!= null )
{ |
07 |
Log.i( "getLocation" , "Location
changed : Lat: " +
location.getLatitude() + "
Lon: " +
location.getLongitude()); |
08 |
double latitude
= location.getLatitude(); |
09 |
double longitude
= location.getLongitude(); |
10 |
double altitude
= location.getAltitude(); |
我們可以通過下面的方法來取得上一次獲得的位置信息:
1 |
Location
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
最後,需要注意的是,要在AndroidManifest.xml中添加權限設置:
1 |
< uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> |
二、LocationManager類使用
下面以一個完整的Android工程代碼作爲示例,介紹瞭如何使用獲得當前設備位置信息。該Demo通過調用LocationManager的getLastKnownLocation方法,顯示設備的經度和緯度信息。
JAVA代碼如下:
01 |
package com.devdiv.test.locationmanager_test; |
03 |
import android.app.Activity; |
04 |
import android.content.Context; |
05 |
import android.location.Location; |
06 |
import android.location.LocationManager; |
07 |
import android.os.Bundle; |
08 |
import android.widget.TextView; |
10 |
public class LocationManager_TestActivity extends Activity
{ |
11 |
/**
Called when the activity is first created. */ |
13 |
public void onCreate(Bundle
savedInstanceState) { |
14 |
super .onCreate(savedInstanceState); |
15 |
setContentView(R.layout.main); |
17 |
LocationManager
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
18 |
Location
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
20 |
updateWithNewLocation(mLocation); |
23 |
private void updateWithNewLocation(Location
location) { |
25 |
String
mLocationInfo = null ; |
27 |
if (location
!= null )
{ |
28 |
double lat
= location.getLatitude(); |
29 |
double lon
= location.getLongitude(); |
30 |
mLocationInfo
= "Latitude:" +
lat + "\n" + "Longitude:" +
lon; |
32 |
mLocationInfo
= "No
location found" ; |
35 |
((TextView
)findViewById(R.id.txt_location_info)).setText(mLocationInfo); |
注意:需要在AndroidManifest.xml中配置相應權限:
1 |
< uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> |
三、LocationManager類使用深入
下面的代碼展示瞭如何使用LocationListener來獲得位置信息更新。
JAVA代碼如下:
01 |
package com.devdiv.test.locationmanager_test2; |
03 |
import android.app.Activity; |
04 |
import android.content.Context; |
05 |
import android.location.Location; |
06 |
import android.location.LocationListener; |
07 |
import android.location.LocationManager; |
08 |
import android.location.LocationProvider; |
09 |
import android.os.Bundle; |
10 |
import android.util.Log; |
11 |
import android.widget.TextView; |
12 |
import android.widget.Toast; |
14 |
public class LocationManager_Test2Activity extends Activity implements LocationListener
{ |
16 |
LocationManager
mLocationManager; |
21 |
/**
Called when the activity is first created. */ |
23 |
public void onCreate(Bundle
savedInstanceState) { |
24 |
super .onCreate(savedInstanceState); |
25 |
setContentView(R.layout.main); |
27 |
mTextView
= (TextView) findViewById(R.id.txt_location_info); |
28 |
mLocationManager
= (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
30 |
Location
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
31 |
if (mLocation
!= null )
{ |
32 |
mTextView.setText( "Latitude:" +
mLocation.getLatitude() + "\n" + "Longitude:" +
mLocation.getLongitude()); |
34 |
mTextView.setText( "Location
not found!" ); |
38 |
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000 , 0 , this ); |
48 |
public void onStatusChanged(String
provider, int status,
Bundle extras) { |
50 |
Log.d( "test" , "onStatusChanged" ); |
53 |
Toast.makeText( this , "onStatusChanged" ,
Toast.LENGTH_SHORT).show(); |
55 |
if (status
== LocationProvider.AVAILABLE) { |
57 |
} else if (status
== LocationProvider.OUT_OF_SERVICE) { |
59 |
} else if (status
== LocationProvider.TEMPORARILY_UNAVAILABLE) { |
66 |
public void onProviderEnabled(String
provider) { |
68 |
Log.d( "test" , "onProviderEnabled" ); |
72 |
public void onProviderDisabled(String
provider) { |
74 |
Log.d( "test" , "onProviderDisabled" ); |
78 |
public void onLocationChanged(Location
location) { |
80 |
Log.d( "test" , "onLocationChanged" ); |
85 |
mTextView.setText( "Latitude:" +
location.getLatitude() + "\n" + "Longitude:" +
location.getLongitude() + "\n" + "onLocationChanged" ); |
注意:需要在AndroidManifest.xml中配置相應權限:
1 |
<uses-permission
android:name= "android.permission.ACCESS_FINE_LOCATION" />
|