Android GPS定位详解

电脑杂谈  发布时间:2020-01-06 21:02:21  来源:网络整理

android gps开发_android 外置gps模块_gps设备开发

自己项目用到了 ,懒得写,偶尔看到这个写的不错,就存下去了

一、LocationManager

LocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。我们可以借助如下代码构建LocationManger对象。

[java]view plaincopyPRint?LocationMangerlocationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

二、LocationListener

LocationListener,位置,位置变化,设施开关与状况。

[java]view plaincopyprint?privateLocationListenerlocationListener=newLocationListener(){/***位置信息变化时触发*/publicvoidonLocationChanged(Locationlocation){updateView(location);Log.i(TAG,"时间:"+location.getTime());Log.i(TAG,"经度:"+location.getLongitude());Log.i(TAG,"纬度:"+location.getLatitude());Log.i(TAG,"海拔:"+location.getAltitude());}/***GPS状态变化时触发*/publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){switch(status){//GPS状态为可见时caseLocationProvider.AVAILABLE:Log.i(TAG,"当前GPS状态为可见状态");break;//GPS状态为服务区外时caseLocationProvider.OUT_OF_SERVICE:Log.i(TAG,"当前GPS状态为服务区外状态");break;//GPS状态为暂停服务时caseLocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG,"当前GPS状态为暂停服务状况");break;}}/***GPS开启时触发*/publicvoidonProviderEnabled(Stringprovider){Locationlocation=lm.getLastKnownLocation(provider);updateView(location);}/***GPS禁用时触发*/publicvoidonProviderDisabled(Stringprovider){updateView(null);}};

三、Location

gps设备开发_android gps开发_android 外置gps模块

Location,位置信息,通过Location可以获得时间、经纬度、海拔等位置信息。上面采取locationListener里面的onLocationChanged()来获得location,下面讲述如何主动获得location。

[java]view plaincopyprint?Locationlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);system.out.println("时间:"+location.getTime());system.out.println("经度:"+location.getLongitude());

注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获得的location的各个参数值都是为0。

四、GpsStatus.Listener

GpsStatus.Listener ,GPS状态,包括GPS启动、停止、第一次定位、卫星变化等事件。

[java]view plaincopyprint?//状态GpsStatus.Listenerlistener=newGpsStatus.Listener(){publicvoidonGpsStatusChanged(intevent){switch(event){//第一次定位caseGpsStatus.GPS_EVENT_FIRST_FIX:Log.i(TAG,"第一次定位");break;//卫星状态改变caseGpsStatus.GPS_EVENT_SATELLITE_STATUS:Log.i(TAG,"卫星状态改变");//获取当前状态GpsStatusgpsStatus=lm.getGpsStatus(null);//获取卫星颗数的默认最大值intmaxSatellites=gpsStatus.getMaxSatellites();//创建一个迭代器保存所有卫星Iterator<GpsSatellite>iters=gpsStatus.getSatellites().iterator();intcount=0;while(iters.hasNext()&&count<=maxSatellites){GpsSatellites=iters.next();count++;}System.out.println("搜索到:"+count+"颗卫星");break;//定位启动caseGpsStatus.GPS_EVENT_STARTED:Log.i(TAG,"定位启动");break;//定位结束caseGpsStatus.GPS_EVENT_STOPPED:Log.i(TAG,"定位结束");break;}};};//绑定状态lm.addGpsStatusListener(listener);

五、GpsStatus

GpsStatus,GPS状态信息,上面在卫星状况差异时,我们就用到了GpsStatus。

gps设备开发_android gps开发_android 外置gps模块

[java]view plaincopyprint?//实例化GpsStatusgpsStatus=locationManager.getGpsStatus(null);//获取当前状况//获取默认最大卫星数intmaxSatellites=gpsStatus.getMaxSatellites();//获取第一次定位时间(启动至第一次定位)intcostTime=gpsStatus.getTimeToFirstFix();//获取卫星Iterable<GpsSatellite>iterable=gpsStatus.getSatellites();//一般重新转换成IteratorIterator<GpsSatellite>itrator=iterable.iterator();

六、GpsSatelliteGpsSatellite,定位卫星,包含卫星的方位、高度、伪随机噪声码、信噪比等信息。

[java]view plaincopyprint?//获取卫星Iterable<GpsSatellite>iterable=gpsStatus。getSatellites();//再次转换成IteratorIterator<GpsSatellite>itrator=iterable。iterator();//通过遍历重新整理为ArrayListArrayList<GpsSatellite>satelliteList=newArrayList<GpsSatellite>();intcount=0;intmaxSatellites=gpsStatus。getMaxSatellites();while(itrator。hasNext()&&count<=maxSatellites){GpsSatellitesatellite=itrator。next();satelliteList。add(satellite);count++;}System。out。println("总共搜索到"+count+"颗卫星");//输出卫星信息for(inti=0;i<satelliteList。

size();i++){//卫星的方位角android gps开发android gps开发,浮点型数据System。out。println(satelliteList。get(i)。getAzimuth());//卫星的高度,浮点型数据System。out。println(satelliteList。get(i)。getElevation());//卫星的伪随机噪声码,整形数据System。out。println(satelliteList。get(i)。getPrn());//卫星的占空比,浮点型数据System。out。println(satelliteList。get(i)。getSnr());//卫星是否有年历表,布尔型数据System。out。println(satelliteList。get(i)。hasAlmanac());//卫星是否有星历表,布尔型数据System。out。println(satelliteList。get(i)。hasEphemeris());//卫星是否被用于最近的GPS修正计算System。out。println(satelliteList。get(i)。hasAlmanac());}

为了利于理解,接下来模拟一个案例,如何在程序代码中使用GPS获取位置信息。

第一步:新建一个Android工程项目,命名为mygps,目录结构如下

第二步:修改main.xml布局文件,修改内容如下:

gps设备开发_android gps开发_android 外置gps模块

[html]view plaincopyprint?<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android=";android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:editable="false"android:id="@+id/editText"/></LinearLayout>

第三步:实用Adnroid平台的GPS设备,需要添加上权限

[html]view plaincopyprint?<uses-permissionandroid:name="android.permission.access_FINE_LOCATION"/><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>

第四步:修改核心模块activity,修改内容如下

[java]view plaincopyprint?packagejason。gprs;importjava。util。Iterator;importandroid。app。Activity;importandroid。content。Context;importandroid。content。Intent;importandroid。location。Criteria;importandroid。location。GpsSatellite;importandroid。location。GpsStatus;importandroid。location。Location;importandroid。location。LocationListener;importandroid。location。LocationManager;importandroid。location。LocationProvider;importandroid。os。Bundle;importandroid。provider。Settings;importandroid。util。Log;importandroid。widget。


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shumachanpin/article-136005-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...