1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java程序调用百度Geocoding API逆地址解析通过经纬度查询位置

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

时间:2019-04-21 14:30:45

相关推荐

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./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", "");

至此,结束!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。