Android LBS系列03 Geocoder类与地址显示
Displaying the Location Address
通过位置更新得到的数据是经纬度坐标值,这种格式在计算距离或者在地图上显示图钉标记很有用,但是这种十进制数字对于大多数终端用户来说没有什么意义,如果需要向用户显示一个地点,更好的做法是显示这个地点的地址信息。
Geocoder类简介
Geocoder可以在街道地址和经纬度坐标之间进行转换。这个类主要用于处理两种编码:
Geocoding(前向地理编码):将街道地址或一个位置的其他描述转换成一个(纬度,经度)坐标值。
Reverse Geocoding(反向地理编码):将一个(纬度,经度)坐标值转换成一个地址值。
进行Reverse Geocoding
Geocoder
API 提供了进行这项工作的方法(getFromLocation()),但是需要注意到这个API是依赖于web服务的。
如果这项服务在设备上不可用,API将会抛出Service not Available exception异常,或者返回一个空的list。
isPresent()
方法自从Android 2.3 (API level 9)开始被加入,这个方法用于检测服务是否存在。
由于getFromLocation()方法是同步的,因此,它们进行查找时将会阻碍线程,所以不应该放入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<Location, Void, Void> { 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<Address> 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
http://developer.android.com/training/basics/location/geocoding.html
Geocoder类:
http://developer.android.com/reference/android/location/Geocoder.html
AsyncTask类:
http://developer.android.com/reference/android/os/AsyncTask.html
博客:Location服务之Geocoder
http://blog.csdn.net/liuhe688/article/details/6566505
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了