本文實例講述了Android編程獲取GPS數據的方法。分享給大家供大家參考,具體如下:
GPS是Android系統中重要的組成部分,通過它可以衍生出眾多的與位置相關的應用。
Android的GPS有一個專門的管理類,稱為LocationManager,所有的GPS定位服務都由其對象產生并進行控制。
首先需要明確的是,LocationManager類的對象獲取并不是直接創建的,而是由系統提供的,具體來說,通過如下方法,為一個LocationManager對象建立一個對象引用:
至此,我們可以用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對象:
我們還可以調用以下函數,對每次更新的位置信息進行我們想要的操作:
其中,第一個參數是LocationProvider對象,第二個參數是刷新的時間差,這里設定為1秒,第三個參數是位置差,這里設定為10米,第四個參數為一個位置監聽器對象,它必須實現4個方法:
①. public void onLocationChanged(Location location)
②. public void onProviderDisabled(String provider)
③. public void onProviderEnabled(String provider)
④. 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中設置許可:
PS:關于AndroidManifest.xml詳細內容可參考本站在線工具:
Android Manifest功能與權限描述大全:
http://tools.VeVB.COm/table/AndroidManifest
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答