Android 逆地理編碼geocoding

來源:互聯網
上載者:User

標籤:

       地理編碼和逆地理編碼都差不多,這裡總結一下逆地理編碼,根據經緯度解析出地址。Android這邊逆地理編碼方式主要是兩種(不包含高德,百度)一種是使用Android 系統的Geocoder類來解析(這種方法過時而且不實用,經常失敗,在中國和印尼都有測試資料,而且部分手機閹割了,這種方法如果想要瞭解請參考http://dev.10086.cn/news/technologyproduct/4859.html);另外一種是使用Google 的web api;

        請開VPN點擊體驗:http://maps.google.com/maps/api/geocode/json?latlng=39.910093,116.403945&language=zh-CN&sensor=false;詳細的參數請參見Google 官方說明:https://developers.google.com/maps/documentation/geocoding/intro?hl=zh-cn;這裡介紹Android的使用:

1.用於結果的回調public interface LocationCallBack { void geoCodeSuccess(String address); void geoCodeFailure(String address); }
2.geocode 的封裝/** *  *  * @author [email protected] */public class BipGeoCoder { private List<RealTimeAddrGeoDataBean> realTimeAddrGeoDataBeans=null; private RequestQueue mQueue = null; private Context context; private String address;     public BipGeoCoder(Context context) {  super();  this.context = context; } /**     * 擷取地址通過Web service     *      */       public void getAddress(final double latitude, final double longitude,final String defAddress,final LocationCallBack locationCallBack){                  String requestUrl=VolleyParamsBuild.newBuilder().putParam("language", "en-ww")  //en-ww 可以根據商務邏輯替換為不同的國家語言                                                  .putParam("latlng", latitude+","+longitude)                                                  .build().toString();        address=defAddress;       mQueue=GetHttpUtils.getInstance(context).getRequestQueue();  JsonObjectRequest stringRequest = new JsonObjectRequest(requestUrl, null,    new Response.Listener<JSONObject>() {     @Override     public void onResponse(JSONObject response) {         TypeFactory tf =  JsonUtil.getMapper().getTypeFactory();         JavaType jt = tf.constructParametricType(List.class, RealTimeAddrGeoDataBean.class);         try {        String json = response.getString("results").toString();       realTimeAddrGeoDataBeans = JsonUtil.getMapper().readValue(json, jt);         } catch (JsonParseException e) {          e.printStackTrace();         } catch (JsonMappingException e) {          e.printStackTrace();         } catch (IOException e) {          e.printStackTrace();         } catch (JSONException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }                              if(realTimeAddrGeoDataBeans!=null&&realTimeAddrGeoDataBeans.size()>0){          address=realTimeAddrGeoDataBeans.get(0).getFormatted_address();          //這裡應該控制再次訪問重新整理地址。         }                    locationCallBack.geoCodeSuccess(address);//         Log.d("Addr","Geocoding:"+address);     }    }, new Response.ErrorListener() {     @Override     public void onErrorResponse(VolleyError error) {         locationCallBack.geoCodeFailure(address);      Log.e("Addr",error.toString());     }    });  mQueue.add(stringRequest);    }  }
3.使用Demo new BipGeoCoder(getActivity()).getAddress(obj.getLatitude(), obj.getLongitude(), locationdef,new LocationCallBack() {      @Override   public void geoCodeSuccess(String address) {    // TODO Auto-generated method stub    Log.e("Addr","geoCodeSuccess:"+address);      }      @Override   public void geoCodeFailure(String address) {    // TODO Auto-generated method stub    Log.e("Addr","geoCodeFailure:"+address);   }   });

最後:

以下內容來自Googlehttps://developers.google.com/maps/documentation/geocoding/intro?hl=zh-cn ,使用前請翻牆

-----------------------------------------------------------

The Google Maps Geocoding API

This service is also available as part of the Google Maps JavaScript API, or the Java and Python client libraries.

What is Geocoding?

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers on a map, or position the map.

Reverse geocoding is the process of converting geographic coordinates into a human-readable address. The Google Maps Geocoding API‘s reverse geocoding service also lets you find the address for a given place ID.

The Google Maps Geocoding API provides a direct way to access these services via an HTTP request.

Before You Begin

This document is intended for website and mobile developers who want to use geocoding data within maps provided by one of the Google Maps APIs.

This service is generally designed for geocoding static (known in advance) addresses for placement of application content on a map; this service is not designed to respond in real time to user input, for example. For dynamic geocoding (for example, within a user interface element), consult the documentation for the Google Maps JavaScript API Client Geocoder and/or the Google Play services Location APIs.

Geocoding is a time and resource intensive task. Whenever possible, pre-geocode known addresses (using the Google Maps Geocoding API described here or another geocoding service), and store your results in a temporary cache of your own design.

To use the Google Maps Geocoding API, you need an API key.

GET A KEY USAGE LIMITS

Google Maps Geocoding API Request Format

A Google Maps Geocoding API request must be of the following form:

https://maps.googleapis.com/maps/api/geocode/output?parameters

where output may be either of the following values:

  • json (recommended) indicates output in JavaScript Object Notation (JSON)

  • xml indicates output as XML

To access the Google Maps Geocoding API over HTTP, use:

http://maps.googleapis.com/maps/api/geocode/output?parameters

HTTP is not recommended for applications that include sensitive user data, such as a user‘s location, in requests.

Some parameters are required while some are optional. As is standard in URLs, parameters are separated using the ampersand (&) character.

Google Maps API for Work users must include valid client and signature parameters with their Geocoding requests. Please refer to Google Maps API for Work Web Services for more information.

The rest of this page describes geocoding and reverse geocoding separately, because different parameters are available for each type of request.

Geocoding (Latitude/Longitude Lookup)

Required parameters in a geocoding request:

  • address — The street address that you want to geocode, in the format used by the national postal service of the country concerned. Additional address elements such as business names and unit, suite or floor numbers should be avoided. Please refer to the FAQ for additional guidance. 
         or 
    components — A component filter for which you wish to obtain a geocode. See Component Filtering for more information. The components filter will also be accepted as an optional parameter if an address is provided.

  • key — Your application‘s API key. This key identifies your application for purposes of quota management. Learn how to get a key.

Optional parameters in a geocoding request:

  • bounds — The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Viewport Biasing below.)

  • language — The language in which to return results. See the list of supported domain languages. Note that we often update supported languages so this list may not be exhaustive. Iflanguage is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible.

  • region — The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.)

  • components — The component filters, separated by a pipe (|). Each component filter consists of a component:value pair and will fully restrict the results from the geocoder. For more information see Component Filtering, below.

Android 逆地理編碼geocoding

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.