Android LBS系列03 Geocoder类与地址显示

摘要:
显示LocationAddress通过位置更新获得经度和纬度坐标值。这种格式对于计算距离或在地图上显示图钉标记非常有用。然而,对于大多数最终用户来说,这个十进制数字是没有意义的。如果需要向用户显示位置,最好显示该位置的地址信息。Geocoder类简介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

 

 

免责声明:文章转载自《Android LBS系列03 Geocoder类与地址显示》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(1)OracleClient数据库操作(淘汰)性能调优之top命令详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

VMP加壳(三):VMP壳爆破实战-破解某编辑类软件

  这次爆破的是某编辑类软件,版本是32位绿色版本:V4.3.1      1、OD打开后发现了VMP0段,这里下个内存访问断点:      又来到这里了,非常明显的VMP入口特征:      一大堆push指令又开始保存物理寄存器;同时让esi指向虚拟指令集;和上面一篇文章分析的混淆手法一模一样,个人猜测用的VMP也是3.5.0版本的;    分配虚拟栈...

django文件上传地址以及media的设置

一、关于media的设置 django的media文件地址的设置其实是类似于static文件地址的设置。 media主要是用来存放一些用户上传的文件(头像、图片等) 分为三个部分: 一是在proj中建立一个media文件夹,文件夹名字可任意取,为方便起见,此处就取名为media。 二是在setting中设置MEDIA_ROOT和MEDIA_URL #用户...

每个文件之间延迟启动批处理bat方法之一

每个文件之间延迟启动批处理bat方法之一 知识点:xp程序中通过ping 127.0.0.1 -n 20 来实现延时操作,ping本地地址20行。            win7中通过timeout 20 来实现延时20秒。 新建批处理bat文件,  .txt→.bat 改扩展名或txt里另存为.bat就可以 例B是路径名包含空格类的需要用引号" "否则会...

jQuery在线引用地址(全)

1.官网jquery压缩版引用地址:  3.1.1版本:   <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>    3.0.0版本:   <script src="https://code.jquery.com/jquery-3.0.0.m...

FSMC 总结

http://www.360doc.com/content/11/1206/15/7736891_170121962.shtml uint16 *data1,*data2; uint32 Yxdata; #defineBank1_YX1_Get(data1) *data1=(*(volatile uint16 *)(0x60006000)) #define...

第一篇-ubuntu18.04访问共享文件夹

1. 在访问Windows共享资料之前,请确保Windows共享是可用的。Linux访问Windows共享或者LInux共享资料给Windows时,都使用Samba软件 rpm -qa | grep samba-client查看samba-client是否安装。如果没有, sudo apt-get install smbclient安装 2. 查看该用户共...