(结果显示如下)
http://api.map.baidu.com/geocoder/v2/?ak=您的密钥& callback=renderReverse&location=28.696117,115.958458&output=json&pois=1
坐标:(115.95845796638,28.696117043877)对应的地址是: 江西省南昌市青山湖区创新路1号
该点周边100米内有10个poi
状态字段:
名称 | 类型 | 说明 |
---|---|---|
status | constant | 返回结果状态值, 成功返回0,其他值请查看附录。 |
location | lat | 纬度坐标 |
lng | 经度坐标 | |
formatted_address | 结构化地址信息 | |
business | 所在商圈信息,如 “人民大学,中关村,苏州街” | |
addressComponent | city | 城市名 |
district | 区县名 | |
province | 省名 | |
street | 街道名 | |
street_number | 街道门牌号 | |
pois(周边poi数组) | addr | 地址信息 |
cp | 数据来源 | |
distance | 离坐标点距离 | |
name | poi名称 | |
poiType | poi类型,如’ 办公大厦,商务大厦’ | |
point | poi坐标{x,y} | |
tel | 电话 | |
uid | poi唯一标识 | |
zip | 邮编 |
json示例:
http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=json&pois=1
xml示例:
http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=xml&pois=1
特别说明:
1.因为Geocoding和反Geocoding使用的门址数据以及算法都不是一样的,所以会出现不能一一对应的现象。
2.逆地址解析location参数传入的参数格式是(纬度lat,经度lng)。
8.返回码状态表
返回码 | 定义 |
---|---|
0 | 正常 |
1 | 服务器内部错误 |
2 | 请求参数非法 |
3 | 权限校验失败 |
4 | 配额校验失败 |
5 | ak不存在或者非法 |
101 | 服务禁用 |
102 | 不通过白名单或者安全码不对 |
2xx | 无权限 |
3xx | 配额错误 |
解析json
{"status":0,"result":{"location":{"lng":115.95845796638,"lat":28.696117043877},"formatted_address":"江西省南昌市青山湖区创新路1号","business":"高新开发区,火炬广场,发展路","addressComponent":{"city":"南昌市","district":"青山湖区","province":"江西省","street":"创新路","street_number":"1号"},"cityCode":163}}
附上代码
package com.xuyw.wx.util; import net.sf.json.JSONObject; import com.xuyw.wx.config.Config; /** * 百度工具类 * * @author xuyw * @email [email protected] * @date 2014-06-22 */ public class BaiDuUtil { public static String getCity(String lat, String lng) { JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result") .getJSONObject("addressComponent"); return obj.getString("city"); } public static JSONObject getLocationInfo(String lat, String lng) { String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + "," + lng + "&output=json&ak=" + Config.BAIDU_GEOCONV_KEY+"&pois=0"; JSONObject obj = JSONObject.fromObject(HttpUtil.getRequest(url)); return obj; } public static void main(String[] args) { System.out.println(BaiDuUtil.getCity("28.694439", "115.939728")); } }
package com.xuyw.wx.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * Http 简单封装 * * @author xuyw * @email [email protected] * @date 2012-06-14 */ public class HttpUtil { // 连接超时时间 private static final int CONNECTION_TIMEOUT = 3000; //读取超时时间 private static final int READ_TIMEOUT = 5000; // 参数编码 private static final String ENCODE_CHARSET = "utf-8"; /** * 发送HTTP_POST请求 * * @see 本方法默认的连接和读取超时均为30秒 * @param reqURL * 请求地址 * @param params * 发送到远程主机的正文数据[a:1,b:2] * @return String */ public static String postRequest(String reqURL, String... params) { StringBuilder resultData = new StringBuilder(); URL url = null; try { url = new URL(reqURL); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection urlConn = null; InputStreamReader in = null; BufferedReader buffer = null; String inputLine = null; DataOutputStream out = null; if (url != null) { try { urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoInput(true);// 设置输入流采用字节流 urlConn.setDoOutput(true);// 设置输出流采用字节流 urlConn.setRequestMethod("POST"); urlConn.setUseCaches(false); // POST请求不能使用缓存 urlConn.setInstanceFollowRedirects(true); urlConn.setConnectTimeout(CONNECTION_TIMEOUT);// 设置连接超时 urlConn.setReadTimeout(READ_TIMEOUT); // 设置读取超时 // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConn.setRequestProperty("Charset", ENCODE_CHARSET);// String param = sendPostParams(params); urlConn.setRequestProperty("Content-Length", param.getBytes().length + "");// // urlConn.setRequestProperty("Connection", "Keep-Alive"); // //设置长连接 urlConn.connect();// 连接服务器发送消息 if (!"".equals(param)) { out = new DataOutputStream(urlConn.getOutputStream()); // 将要上传的内容写入流中 out.writeBytes(param); // 刷新、关闭 out.flush(); out.close(); } in = new InputStreamReader(urlConn.getInputStream(), HttpUtil.ENCODE_CHARSET); buffer = new BufferedReader(in); if (urlConn.getResponseCode() == 200) { while ((inputLine = buffer.readLine()) != null) { resultData.append(inputLine); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (buffer != null) { buffer.close(); } if (in != null) { in.close(); } if (urlConn != null) { urlConn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } } return resultData.toString(); } /** * 发送HTTP_GET请求 * * @see 本方法默认的连接和读取超时均为30秒 * @param httpUrl * 请求地址 * @param map * 发送到远程主机的正文数据[a:1,b:2] * @return String */ public static String getRequest(String httpUrl, String... params) { StringBuilder resultData = new StringBuilder(); URL url = null; try { String paramurl = sendGetParams(httpUrl, params); url = new URL(paramurl); } catch (MalformedURLException e) { e.printStackTrace(); } HttpURLConnection urlConn = null; InputStreamReader in = null; BufferedReader buffer = null; String inputLine = null; if (url != null) { try { urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("GET"); urlConn.setConnectTimeout(CONNECTION_TIMEOUT); in = new InputStreamReader(urlConn.getInputStream(), HttpUtil.ENCODE_CHARSET); buffer = new BufferedReader(in); if (urlConn.getResponseCode() == 200) { while ((inputLine = buffer.readLine()) != null) { resultData.append(inputLine); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (buffer != null) { buffer.close(); } if (in != null) { in.close(); } if (urlConn != null) { urlConn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } } return resultData.toString(); } /** * Post追加参数 * * @see <code>params</code> * @param reqURL * 请求地址 * @param params * 发送到远程主机的正文数据[a:1,b:2] * @return */ private static String sendPostParams(String... params) { StringBuilder sbd = new StringBuilder(""); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { String[] temp = params[i].split(":"); sbd.append(temp[0]); sbd.append("="); sbd.append(urlEncode(temp[1])); sbd.append("&"); } sbd.setLength(sbd.length() - 1);// 删掉最后一个 } return sbd.toString(); } /** * Get追加参数 * * @see <code>params</code> * @param reqURL * 请求地址 * @param params * 发送到远程主机的正文数据[a:1,b:2] * @return */ private static String sendGetParams(String reqURL, String... params) { StringBuilder sbd = new StringBuilder(reqURL); if (params != null && params.length > 0) { if (isexist(reqURL, "?")) {// 存在? sbd.append("&"); } else { sbd.append("?"); } for (int i = 0; i < params.length; i++) { String[] temp = params[i].split(":"); sbd.append(temp[0]); sbd.append("="); sbd.append(urlEncode(temp[1])); sbd.append("&"); } sbd.setLength(sbd.length() - 1);// 删掉最后一个 } return sbd.toString(); } // 查找某个字符串是否存在 private static boolean isexist(String str, String fstr) { return str.indexOf(fstr) == -1 ? false : true; } /** * 编码 * * @param source * @return */ private static String urlEncode(String source) { String result = source; try { result = java.net.URLEncoder .encode(source, HttpUtil.ENCODE_CHARSET); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10366.html