【android开发wiki】进阶篇making your app location aware(三):displaying the location address

【android开发wiki】进阶篇making your app location aware(三):displaying the location address

ID:34102768

大小:270.57 KB

页数:3页

时间:2019-03-03

【android开发wiki】进阶篇making your app location aware(三):displaying the location address_第1页
【android开发wiki】进阶篇making your app location aware(三):displaying the location address_第2页
【android开发wiki】进阶篇making your app location aware(三):displaying the location address_第3页
资源描述:

《【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

addresses=null;31.try{32.//CallthesynchronousgetFromLocation()metho

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

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。