java程序调用百度Geocoding API逆地址解析通过经纬度查询位置

自从百度升级了自己的逆地址解析调用接口,就多了一些调用限制,具体参数可以参照百度给出的解释。本文主要研究通过java代码调用该接口:

下面给出调用接口的方法:

public static String getAddress(double lat, double lng, String coord_type) throws Exception{         String result = null;         HttpClient httpClient = new DefaultHttpClient();         HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);         String uri = "http://api.map.baidu.com/geocoder/v2/?ak=yourak

                           &   location="+lat+","+lng+"&output=json&pois=0&coordtype="+coord_type;         HttpGet get = new HttpGet(uri); // 发送get请求         HttPResponse response = httpClient.execute(get);         if (response.getStatusLine().getStatusCode() == 200) { // http请求正常             result = new String(StreamTools.read(response.getEntity().getContent()), "UTF-8");             //System.out.print(result);         }         return result;     }

其中ak=yourak为在百度申请的秘钥,coord_type为传入的坐标系类型。这些细节性的东西都可以在百度自己的百度地图API里找到。

代码执行完成后,得到的result即为json类型的结果,下面处理得到的结果:

                        JSONObject jsonObject = JSONObject.fromObject(addr);               JSONObject result = jsonObject.getJSONObject("result");               String address = result.getString("formatted_address"); // 全地址               JSONObject addrComponent = result.getJSONObject("addressComponent");               String province = addrComponent.optString("province", ""); // 省               String city = addrComponent.optString("city", ""); // 城市               String district = addrComponent.optString("district", ""); // 区               String cityCode = result.optString("cityCode", "");

至此,结束!