博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android LBS系列03 Geocoder类与地址显示
阅读量:4611 次
发布时间:2019-06-09

本文共 3010 字,大约阅读时间需要 10 分钟。

Displaying the Location Address

 

  通过位置更新得到的数据是经纬度坐标值,这种格式在计算距离或者在地图上显示图钉标记很有用,但是这种十进制数字对于大多数终端用户来说没有什么意义,如果需要向用户显示一个地点,更好的做法是显示这个地点的地址信息。

 

Geocoder类简介

  Geocoder可以在街道地址和经纬度坐标之间进行转换。这个类主要用于处理两种编码:

  Geocoding前向地理编码):将街道地址或一个位置的其他描述转换成一个(纬度,经度)坐标值。

  Reverse Geocoding反向地理编码):将一个(纬度,经度)坐标值转换成一个地址值。

 

进行Reverse Geocoding

   API 提供了进行这项工作的方法(),但是需要注意到这个API是依赖于web服务的。

  如果这项服务在设备上不可用,API将会抛出Service not Available exception异常,或者返回一个空的list。

   方法自从Android 2.3 (API level 9)开始被加入,这个方法用于检测服务是否存在。

  由于方法是同步的,因此,它们进行查找时将会阻碍线程,所以不应该放入UI线程,应该放入后台。这里可以参考一下最后给出的博客链接中的详述。

 

private final LocationListener listener = new LocationListener() {    public void onLocationChanged(Location location)     {        // Bypass reverse-geocoding if the Geocoder service is not available on the        // device. The isPresent() convenient method is only available on Gingerbread or above.        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent())         {            // Since the geocoding API is synchronous and may take a while.  You don't want to lock            // up the UI thread.  Invoking reverse geocoding in an AsyncTask.            (new ReverseGeocodingTask(this)).execute(new Location[] {location});        }    }    ...};// AsyncTask encapsulating the reverse-geocoding API.  Since the geocoder API is blocked,// we do not want to invoke it from the UI thread.private class ReverseGeocodingTask extends AsyncTask
{ Context mContext; public ReverseGeocodingTask(Context context) { super(); mContext = context; } @Override protected Void doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); Location loc = params[0]; List
addresses = null; try { // Call the synchronous getFromLocation() method by passing in the lat/long values. addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); // Update UI field with the exception. Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget(); } if (addresses != null &s;&s; addresses.size() > 0) { Address address = addresses.get(0); // Format the first line of address (if available), city, and country name. String addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); // Update the UI via a message handler. Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget(); } return null; }}

 

参考资料

  Training: Displaying the Location Address

  

  Geocoder类:

  

  AsyncTask类:

  

 

  博客:Location服务之Geocoder

  

 

 

转载于:https://www.cnblogs.com/mengdd/archive/2013/01/12/2857600.html

你可能感兴趣的文章
布局大全
查看>>
eclipse中安装tomcat插件
查看>>
常见设计模式C++代码实现
查看>>
C++线程同步的四种方式(Windows)
查看>>
前端面试集锦(1)
查看>>
What are Upgrade, Product and Package Codes used for? By pusu
查看>>
【转】梯度下降算法以及其Python实现
查看>>
H5的本地存储
查看>>
1035 Password (20 分)
查看>>
VC静态连接库注意事项
查看>>
并不对劲的hdu4777
查看>>
如何在个人博客首页中添加访问计数器
查看>>
Morning Reading Collection
查看>>
Sudo
查看>>
JS案例之8——从一个数组中随机取数
查看>>
C#中Dictionary小记
查看>>
mysql日期类型默认值'0000-00-00'容错处理
查看>>
openni和骨架追踪 rviz查看---34
查看>>
防止网站被iframe调用
查看>>
B - 畅通工程(并查集)
查看>>