自從百度升級(jí)了自己的逆地址解析調(diào)用接口,就多了一些調(diào)用限制,具體參數(shù)可以參照百度給出的解釋。本文主要研究通過(guò)java代碼調(diào)用該接口:
下面給出調(diào)用接口的方法:
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); // 發(fā)送get請(qǐng)求 HttPResponse response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == 200) { // http請(qǐng)求正常 result = new String(StreamTools.read(response.getEntity().getContent()), "UTF-8"); //System.out.print(result); } return result; }
其中ak=yourak為在百度申請(qǐng)的秘鑰,coord_type為傳入的坐標(biāo)系類型。這些細(xì)節(jié)性的東西都可以在百度自己的百度地圖API里找到。
代碼執(zhí)行完成后,得到的result即為json類型的結(jié)果,下面處理得到的結(jié)果:
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", ""); // 區(qū) String cityCode = result.optString("cityCode", "");
至此,結(jié)束!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注