欢迎来到天天文库
浏览记录
ID:34102768
大小:270.57 KB
页数:3页
时间:2019-03-03
《【android开发wiki】进阶篇making your app location aware(三):displaying the location address》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、eoe移动开发者社区http://www.eoe.cn/原文链接:http://docs.eoeandroid.com/training/basics/location/geocoding.html随着上一节的学习,位置更新是以经纬度坐标的形式接收的。这种接收方式适用于计算距离和在地图上显示标志的,十进制数字对于用户来说并没有什么意思,如果你需要显示位置给用户,更好的是显示地址信息。执行反向地理编码反向地理编码是把处理经纬度坐标转换为人们可以读取的地址信息。这里使用的是GeocoderAPI。注意这套API背后以来的是网络
2、服务。如果这个服务在设备里丌可以使用,那么API会抛出服务丌可使用的异常,或者返回一个空的地址列表。在android2.3里增加了一个可用的方法isPresent(),这个方法用于检测是否存在该服务。下边的代码片段就是使用GeocoderAPI来实现反向地理解码。因为double,int)getFromLocation()方法是同步的,你丌应该在UI线程哩调用,因此在下边代码里使用了AsyncTask。1.privatefinalLocationListenerlistener=newLocationListener(){
3、2.3.publicvoidonLocationChanged(Locationlocation){4.//Bypassreverse-geocodingiftheGeocoderserviceisnotavailableonthe5.//device.TheisPresent()convenientmethodisonlyavailableonGingerbreadorabove.6.if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD&&Geocoder.i
4、sPresent()){7.//SincethegeocodingAPIissynchronousandmaytakeawhile.Youdon'twanttolock8.//uptheUIthread.InvokingreversegeocodinginanAsyncTask.eoe移动开发者社区http://www.eoe.cn/9.(newReverseGeocodingTask(this)).execute(newLocation[]{location});10.}11.}12....13.};14.15.//As
5、yncTaskencapsulatingthereverse-geocodingAPI.SincethegeocoderAPIisblocked,16.//wedonotwanttoinvokeitfromtheUIthread.17.privateclassReverseGeocodingTaskextendsAsyncTask{18.ContextmContext;19.20.publicReverseGeocodingTask(Contextcontext){21.super(
6、);22.mContext=context;23.}24.25.@Override26.protectedVoiddoInBackground(Location...params){27.Geocodergeocoder=newGeocoder(mContext,Locale.getDefault());28.29.Locationloc=params[0];30.List
7、dbypassinginthelat/longvalues.33.addresses=geocoder.getFromLocation(loc.getLatitude(),loc.getLongitude(),1);34.}catch(IOExceptione){35.e.printStackTrace();36.//UpdateUIfieldwiththeexception.eoe移动开发者社区http://www.eoe.cn/37.Message.obtain(mHandler,UPDATE_ADDRESS,e.to
8、String()).sendToTarget();38.}39.if(addresses!=null&s;&s;addresses.size()>0){40.Addressaddress=addresses.get(0);41.//Formatthefirstlineofaddress(ifavaila
此文档下载收益归作者所有