Android編程獲取GPS數(shù)據(jù)的方法詳解
本文實例講述了Android編程獲取GPS數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
GPS是Android系統(tǒng)中重要的組成部分,通過它可以衍生出眾多的與位置相關的應用。
Android的GPS有一個專門的管理類,稱為LocationManager,所有的GPS定位服務都由其對象產(chǎn)生并進行控制。
首先需要明確的是,LocationManager類的對象獲取并不是直接創(chuàng)建的,而是由系統(tǒng)提供的,具體來說,通過如下方法,為一個LocationManager對象建立一個對象引用:
至此,我們可以用locationManager這個對象對任意有關GPS的功能進行操作了。下表列出了幾個常用的成員方法:
方法及其簽名 |
描述 |
List<String> getAllProviders() |
獲取所有與設備關聯(lián)的定位模塊的列表 |
String getBestProvider(Criteria, boolean) |
獲取設定的標準(Criteria對象)中最適合的一個設備 |
GpsStatus getGpsStatus(GpsStatus) |
獲取GPS當前狀態(tài) |
Location getLastKnownLocation(String) |
獲取最近一次的可用地點信息 |
boolean isProviderEnabled(String) |
判斷參數(shù)所提及的設備是否可用 |
GPS還有一個支持API,即Location,它的作用是一個代表位置信息的抽象類,用它可以獲取所有的位置數(shù)據(jù):
方法及其簽名 |
描述 |
double getAltitude() |
獲取當前高度 |
float getBearing() |
獲取當前方向 |
double getLatitude() |
獲取當前緯度 |
double getLongitude() |
獲取當前經(jīng)度 |
float getSpeed() |
獲取當前速度 |
我們可以用以上的方法開始進行定位。
可以將地點信息傳遞給一個Location對象:
我們還可以調用以下函數(shù),對每次更新的位置信息進行我們想要的操作:
其中,第一個參數(shù)是LocationProvider對象,第二個參數(shù)是刷新的時間差,這里設定為1秒,第三個參數(shù)是位置差,這里設定為10米,第四個參數(shù)為一個位置監(jiān)聽器對象,它必須實現(xiàn)4個方法:
①. public void onLocationChanged(Location location)
②. public void onProviderDisabled(String provider)
③. public void onProviderEnabled(String provider)
④. public void onStatusChanged(String provider, int status, Bundleextras)
可以重寫這些方法來實現(xiàn)我們的需求。
當我們使用模擬器進行測試的時候,由于模擬器無法獲取地理位置,所以必須用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.jb51.net/table/AndroidManifest
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android控件用法總結》、《Android視圖View技巧總結》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關文章
實現(xiàn)Android studio設置自動導包及自動導包快捷鍵
這篇文章主要介紹了實現(xiàn)Android studio設置自動導包及自動導包快捷鍵的相關資料,需要的朋友可以參考下2017-05-05Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法示例
這篇文章主要介紹了Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法,結合實例形式分析了Android滑動效果的原理及擴展ViewGroup實現(xiàn)滑動功能的相關操作技巧,需要的朋友可以參考下2017-08-08Android AutoCompleteTextView控件基本用法示例
這篇文章主要介紹了Android AutoCompleteTextView控件基本用法,結合實例形式分析了AutoCompleteTextView控件的功能、使用方法及相關操作步驟,需要的朋友可以參考下2016-06-06Android studio實現(xiàn)PopupWindow彈出框效果
這篇文章主要為大家詳細介紹了Android studio實現(xiàn)PopupWindow彈出框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10Android使alertDialog.builder不會點擊外面和按返回鍵消失的方法
本篇文章主要介紹了Android使alertDialog.builder不會點擊外面和按返回鍵消失的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01