zoukankan      html  css  js  c++  java
  • 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

     

     

  • 相关阅读:
    本地http://localhost打不开怎么办
    C#中lock死锁实例教程
    结对-四则运算答题器-项目进度
    Forward团队-爬虫豆瓣top250项目-代码设计规范
    Forward团队-爬虫豆瓣top250项目-设计文档
    学习使用github
    Forward团队-爬虫豆瓣top250项目-团队编程项目开发环境搭建过程
    课后作业-阅读任务-阅读提问-1
    20170915-构建之法:现代软件工程-阅读笔记
    结对-四则运算答题器-设计文档
  • 原文地址:https://www.cnblogs.com/mengdd/p/2857600.html
Copyright © 2011-2022 走看看