【2017年整理】Android之GPS定位详解

上传人:豆浆 文档编号:986736 上传时间:2017-05-24 格式:DOC 页数:11 大小:123.50KB
返回 下载 相关 举报
【2017年整理】Android之GPS定位详解_第1页
第1页 / 共11页
【2017年整理】Android之GPS定位详解_第2页
第2页 / 共11页
【2017年整理】Android之GPS定位详解_第3页
第3页 / 共11页
【2017年整理】Android之GPS定位详解_第4页
第4页 / 共11页
【2017年整理】Android之GPS定位详解_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《【2017年整理】Android之GPS定位详解》由会员分享,可在线阅读,更多相关《【2017年整理】Android之GPS定位详解(11页珍藏版)》请在金锄头文库上搜索。

1、一、LocationManagerLocationMangager,位置管理器。要想操作定位相关设备,必须先定义个 LocationManager。我们可以通过如下代码创建 LocationManger 对象。LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 二、LocationListenerLocationListener,位置监听,监听位置变化,监听设备开关与状态。private LocationListener locationListener

2、=new LocationListener() /* 位置信息变化时触发*/public void onLocationChanged(Location location) updateView(location);Log.i(TAG, 时间:+location.getTime(); Log.i(TAG, 经度:+location.getLongitude(); Log.i(TAG, 纬度:+location.getLatitude(); Log.i(TAG, 海拔:+location.getAltitude(); /* GPS 状态变化时触发*/public void onStatusCha

3、nged(String provider, int status, Bundle extras) switch (status) /GPS 状态为可见时case LocationProvider.AVAILABLE:Log.i(TAG, 当前 GPS 状态为可见状态);break;/GPS 状态为服务区外时case LocationProvider.OUT_OF_SERVICE:Log.i(TAG, 当前 GPS 状态为服务区外状态);break;/GPS 状态为暂停服务时case LocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG, 当前 G

4、PS 状态为暂停服务状态);break; /* GPS 开启时触发*/public void onProviderEnabled(String provider) Location location=lm.getLastKnownLocation(provider);updateView(location);/* GPS 禁用时触发*/public void onProviderDisabled(String provider) updateView(null);三、LocationLocation,位置信息,通过 Location 可以获取时间、经纬度、海拔等位置信息。上面采用locatio

5、nListener 里面的 onLocationChanged()来获取 location,下面讲述如何主动获取 location。Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); system.out.println(时间:+location.getTime(); system.out.println(经度:+location.getLongitude(); 注意:Location location=new Location(LocationManager.GPS_PRO

6、VIDER)方式获取的 location的各个参数值都是为 0。四、GpsStatus.ListenerGpsStatus.Listener ,GPS 状态监听,包括 GPS 启动、停止、第一次定位、卫星变化等事件。/状态监听GpsStatus.Listener listener = new GpsStatus.Listener() public void onGpsStatusChanged(int event) switch (event) /第一次定位case GpsStatus.GPS_EVENT_FIRST_FIX:Log.i(TAG, 第一次定位);break;/卫星状态改变cas

7、e GpsStatus.GPS_EVENT_SATELLITE_STATUS:Log.i(TAG, 卫星状态改变); /获取当前状态GpsStatus gpsStatus=lm.getGpsStatus(null);/获取卫星颗数的默认最大值int maxSatellites = gpsStatus.getMaxSatellites();/创建一个迭代器保存所有卫星 Iterator iters = gpsStatus.getSatellites().iterator();int count = 0; while (iters.hasNext() & count iterable=gpsSta

8、tus.getSatellites(); /一般再次转换成 Iterator Iterator itrator=iterable.iterator(); 六、GpsSatelliteGpsSatellite,定位卫星,包含卫星的方位、高度、伪随机噪声码、信噪比等信息。/获取卫星 Iterable iterable=gpsStatus.getSatellites(); /再次转换成 Iterator Iterator itrator=iterable.iterator(); /通过遍历重新整理为 ArrayList ArrayList satelliteList=new ArrayList();

9、 int count=0; int maxSatellites=gpsStatus.getMaxSatellites(); while (itrator.hasNext() & count 第三步:实用 Adnroid 平台的 GPS 设备,需要添加上权限第四步:修改核心组件 activity,修改内容如下package com.ljq.activity;import java.util.Iterator;import android.app.Activity;import android.content.Context;import android.content.Intent;import

10、 android.location.Criteria;import android.location.GpsSatellite;import android.location.GpsStatus;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.location.LocationProvider;import android.os.Bundle;import android.provide

11、r.Settings;import android.util.Log;import android.widget.EditText;import android.widget.Toast;public class GpsActivity extends Activity private EditText editText;private LocationManager lm;private static final String TAG=GpsActivity;Overrideprotected void onDestroy() / TODO Auto-generated method stu

12、bsuper.onDestroy();lm.removeUpdates(locationListener);Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);editText=(EditText)findViewById(R.id.editText);lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);/判断 GPS 是否正常启动i

13、f(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)Toast.makeText(this, 请开启 GPS 导航., Toast.LENGTH_SHORT).show();/返回开启 GPS 导航设置界面Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent,0); return; /为获取地理位置信息时设置查询条件String bestProvider = lm.getBestProvider(

14、getCriteria(), true);/获取位置信息/如果不设置查询要求, getLastKnownLocation 方法传人的参数为LocationManager.GPS_PROVIDERLocation location= lm.getLastKnownLocation(bestProvider); updateView(location);/监听状态lm.addGpsStatusListener(listener);/绑定监听,有 4 个参数 /参数 1,设备:有 GPS_PROVIDER 和 NETWORK_PROVIDER 两种/参数 2,位置信息更新周期,单位毫秒 /参数 3,

15、位置变化最小距离:当位置距离变化超过此值时,将更新位置信息 /参数 4,监听 /备注:参数 2 和 3,如果参数 3 不为 0,则以参数 3 为准;参数 3 为 0,则通过时间来定时更新;两者为0,则随时刷新 / 1 秒更新一次,或最小位移变化超过 1 米更新一次;/注意:此处更新准确度非常低,推荐在 service 里面启动一个 Thread,在 run 中 sleep(10000);然后执行 handler.sendMessage(),更新位置lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);/位置监听private LocationListener locationListener=new LocationListener() /* 位置信息变化时触发*/public void onLocationChanged(Location location) updateView(location);Log.i(TAG, 时间:+location.getTime(); Log.i(TAG, 经度:+location.getLongitude(); Log.i(TAG, 纬度:+location.getLatitude(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号