您的位置:首页 > 其它

利用百度Geocoding API实现逆地址解析

2015-04-13 22:45 387 查看
Geocoding API 是百度提供的服务接口,

主要是用于提供从地址解析到经纬度坐标或者从经纬度坐标解析到地址的转换服务。主要过程是,用户通过接口发送html请求,并将返回的数据进行解析

http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding具体的百度提供的服务

主要是构建一个具体的url,例http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderOption&output=json&address=百度大厦&city=北京市

返回值即为解析的数据。其中可以使用Json数据,也可以使用xml数据

注意json数据在返回的时候可以选择带不带返回值,不带的话可以直接对返回的数据进行处理。

需要申请百度的ak,这个是需要每个人单独申请的。在申请时注意勾选所有的服务。

//这是构建url的过程,可以利用上一章的坐标值
StringBuilder url=new StringBuilder();
url.append("http://api.map.baidu.com/geocoder/v2/?ak=n3N2Y0yFiflGHmxC6HZHmfoP&location=");
url.append(location.getLatitude());
url.append(",");
url.append(location.getLongitude());
url.append("&output=json&pois=0");
//发送Http请求
HttpClient h = new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url.toString());
HttpResponse httpResponse=h.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
//如果请求成功,取出服务返回的具体内容
//response里为返回的Json数据
String response = EntityUtils.toString(entity);
//使用Json方法解析json数据
SONObject jsonObject=new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("result");
JSONObject addressComponent =          result.getJSONObject("addressComponent");
String city = (String) addressComponent.get("city");
String district = (String) addressComponent.get("district");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: