欢迎来到天天文库
浏览记录
ID:34151675
大小:321.86 KB
页数:5页
时间:2019-03-03
《【android开发wiki】进阶篇making your app location aware(二):obtaining the current location》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、eoe移动开发者社区http://www.eoe.cn/原文链接:http://docs.eoeandroid.com/training/basics/location/currentlocation.html通过LocationManager设置好你的应用之后,你就可以获取位置数据更新了。设置位置监听LocationManager类拥许多方法用于接受位置数据更新,在它里面最简单的形式,注册一个时间监听,用于识别来自位置数据更新的位置管理,以及设置最小时间和间隔距离。随着时间和位置间隔的更新,回调函数onLocatio
2、nChanged()会被调用。在下边的代码中,每隔至少十秒或者距离移动了十米,位置监听函数将要被调用。另外一个通知应用改变状态的回调函数来自位置提供者。1.privatefinalLocationListenerlistener=newLocationListener(){2.3.@Override4.publicvoidonLocationChanged(Locationlocation){5.//Anewlocationupdateisreceived.Dosomethingusefulwithit.Inthisc
3、ase,6.//we'resendingtheupdatetoahandlerwhichthenupdatestheUIwiththenew7.//location.8.Message.obtain(mHandler,9.UPDATE_LATLNG,10.location.getLatitude()+","+11.location.getLongitude()).sendToTarget();12.13....14.}15....16.};17.eoe移动开发者社区http://www.eoe.cn/18.mLocat
4、ionManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,19.10000,//10-secondinterval.20.10,//10meters.21.listener);处理多元位置更新通常来说,使用GPS的位置提供者要比基于网络的位置提供者耗费更多的时间。如果你尽可能快的更新位置数据以及你想让更精确的更新数据变得可能,常见的方法就是注册GPS和网络提供者两种方式的监听器。在onLocationChanged()回调函数里,你将接收来自多个位置提
5、供的的信息,包含带有不同时间戳和不同等级的信息。你需要通过逻辑处理来消除歧义信息以及更新最新数据。下边的代码片段演示了这段逻辑。1.privatestaticfinalintTWO_MINUTES=1000*60*2;2.3./**DetermineswhetheroneLocationreadingisbetterthanthecurrentLocationfix4.*@paramlocationThenewLocationthatyouwanttoevaluate5.*@paramcurrentBestLocati
6、onThecurrentLocationfix,towhichyouwanttocomparethenewone6.*/7.protectedbooleanisBetterLocation(Locationlocation,LocationcurrentBestLocation){8.if(currentBestLocation==null){9.//Anewlocationisalwaysbetterthannolocation10.returntrue;11.}12.13.//Checkwhetherthenewl
7、ocationfixisnewerorolder14.longtimeDelta=location.getTime()-currentBestLocation.getTime();eoe移动开发者社区http://www.eoe.cn/15.booleanisSignificantlyNewer=timeDelta>TWO_MINUTES;16.booleanisSignificantlyOlder=timeDelta<-TWO_MINUTES;17.booleanisNewer=timeDelta>0;18.19./
8、/Ifit'sbeenmorethantwominutessincethecurrentlocation,usethenewlocation20.//becausetheuserhaslikelymoved21.if(isSignificantlyNewer){22.returntrue;23.//Ifthenewlocation
此文档下载收益归作者所有