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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM