Android中如何獲取GPS資料
作者:高天辰
GPS是Android系統中重要的組成部分,通過它可以衍生出眾多的與位置相關的應用。
Android的GPS有一個專門的管理類,稱為LocationManager,所有的GPS定位服務都由其物件產生並進行控制。
首先需要明確的是,LocationManager類的物件獲取並不是直接建立的,而是由系統提供的,具體來說,通過如下方法,為一個LocationManager物件建立一個物件引用:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
至此,我們可以用locationManager這個物件對任意有關GPS的功能進行操作了。下表列出了幾個常用的成員方法:
方法及其簽名 |
描述 |
List<String> getAllProviders() |
獲取所有與裝置關聯的定位模組的列表 |
String getBestProvider(Criteria, boolean) |
獲取設定的標準(Criteria物件)中最適合的一個裝置 |
GpsStatus getGpsStatus(GpsStatus) |
獲取GPS當前狀態 |
Location getLastKnownLocation(String) |
獲取最近一次的可用地點資訊 |
boolean isProviderEnabled(String) |
判斷引數所提及的裝置是否可用 |
GPS還有一個支援API,即Location,它的作用是一個代表位置資訊的抽象類,用它可以獲取所有的位置資料:
方法及其簽名 |
描述 |
double getAltitude() |
獲取當前高度 |
float getBearing() |
獲取當前方向 |
double getLatitude() |
獲取當前緯度 |
double getLongitude() |
獲取當前經度 |
float getSpeed() |
獲取當前速度 |
我們可以用以上的方法開始進行定位。
可以將地點資訊傳遞給一個Location物件:
Locationlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
我們還可以呼叫以下函式,對每次更新的位置資訊進行我們想要的操作:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 10, new LocationListener())
其中,第一個引數是LocationProvider物件,第二個引數是重新整理的時間差,這裡設定為1秒,第三個引數是位置差,這裡設定為10米,第四個引數為一個位置監聽器物件,它必須實現4個方法:
n public void onLocationChanged(Location location)
n public void onProviderDisabled(String provider)
n public void onProviderEnabled(String provider)
n public void onStatusChanged(String provider, int status, Bundleextras)
可以重寫這些方法來實現我們的需求。
當我們使用模擬器進行測試的時候,由於模擬器無法獲取地理位置,所以必須用Emulator的位置控制器進行設定:
最終的結果如圖所示:
程式碼如下所示:
package org.timm.android;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class LocationTryActivity extends Activity {
EditText text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
text = (EditText)findViewById(R.id.textShow);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showLocation(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
showLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
showLocation(null);
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
showLocation(locationManager.getLastKnownLocation(provider));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
});
}
public void showLocation(Location currentLocation){
if(currentLocation != null){
String s = "";
s += " Current Location: (";
s += currentLocation.getLongitude();
s += ",";
s += currentLocation.getLatitude();
s += ")\n Speed: ";
s += currentLocation.getSpeed();
s += "\n Direction: ";
s += currentLocation.getBearing();
text.setText(s);
}
else{
text.setText("");
}
}
}
最後一點需要說明的是,需要在AndroidManifest.xml中設定許可:
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />