Android中如何获取GPS数据

来源:互联网 发布:json解析listview展示 编辑:程序博客网 时间:2024/05/22 09:51

作者:高天辰

         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 stubshowLocation(location);}public void onProviderDisabled(String provider) {// TODO Auto-generated method stubshowLocation(null);}public void onProviderEnabled(String provider) {// TODO Auto-generated method stubshowLocation(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" />


原创粉丝点击