欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程獲取GPS數(shù)據(jù)的方法詳解

 更新時間:2016年10月24日 11:26:33   作者:pku_android  
這篇文章主要介紹了Android編程獲取GPS數(shù)據(jù)的方法,結(jié)合實例形式分析了Android地理位置操作的相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下

本文實例講述了Android編程獲取GPS數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

GPS是Android系統(tǒng)中重要的組成部分,通過它可以衍生出眾多的與位置相關(guān)的應(yīng)用。

Android的GPS有一個專門的管理類,稱為LocationManager,所有的GPS定位服務(wù)都由其對象產(chǎn)生并進行控制。

首先需要明確的是,LocationManager類的對象獲取并不是直接創(chuàng)建的,而是由系統(tǒng)提供的,具體來說,通過如下方法,為一個LocationManager對象建立一個對象引用:

復制代碼 代碼如下:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

至此,我們可以用locationManager這個對象對任意有關(guān)GPS的功能進行操作了。下表列出了幾個常用的成員方法:

方法及其簽名

描述

List<String> getAllProviders()

獲取所有與設(shè)備關(guān)聯(lián)的定位模塊的列表

String getBestProvider(Criteria, boolean)

獲取設(shè)定的標準(Criteria對象)中最適合的一個設(shè)備

GpsStatus getGpsStatus(GpsStatus)

獲取GPS當前狀態(tài)

Location getLastKnownLocation(String)

獲取最近一次的可用地點信息

boolean isProviderEnabled(String)

判斷參數(shù)所提及的設(shè)備是否可用


GPS還有一個支持API,即Location,它的作用是一個代表位置信息的抽象類,用它可以獲取所有的位置數(shù)據(jù):

方法及其簽名

描述

double getAltitude()

獲取當前高度

float getBearing()

獲取當前方向

double getLatitude()

獲取當前緯度

double getLongitude()

獲取當前經(jīng)度

float getSpeed()

獲取當前速度


我們可以用以上的方法開始進行定位。

可以將地點信息傳遞給一個Location對象:

復制代碼 代碼如下:
Locationlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

我們還可以調(diào)用以下函數(shù),對每次更新的位置信息進行我們想要的操作:

復制代碼 代碼如下:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 10, new LocationListener())

其中,第一個參數(shù)是LocationProvider對象,第二個參數(shù)是刷新的時間差,這里設(shè)定為1秒,第三個參數(shù)是位置差,這里設(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的位置控制器進行設(shè)置:

最終的結(jié)果如圖所示:

代碼如下所示:

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中設(shè)置許可:

復制代碼 代碼如下:
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />

PS:關(guān)于AndroidManifest.xml詳細內(nèi)容可參考本站在線工具:

Android Manifest功能與權(quán)限描述大全:

http://tools.jb51.net/table/AndroidManifest

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論