发布于 2017-07-25 21:44:45 | 146 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程获取GPS数据的方法,结合实例形式分析了Android地理位置操作的相关函数与使用技巧,需要的朋友可以参考下

本文实例讲述了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个方法:

①. 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中设置许可:

<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />

PS:关于AndroidManifest.xml详细内容可参考本站在线工具:

Android Manifest功能与权限描述大全:

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务