本文共 2114 字,大约阅读时间需要 7 分钟。
百度API中的全球逆地理编码服务提供将坐标点(经纬度)转换为对应位置信息(所在行政区划,周边地标点分布)功能。
访问的URL是:
http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll &location=纬度,经度 //GET请求
因为使用百度地图的API就要申请AK,申请链接如下
//JSON转化为MAP函数中需要//通过经纬度获得城市信息中需要 net.sf.json-lib json-lib 2.4 jdk15 org.apache.httpcomponents httpclient 4.5.11
通过经纬度得到具体城市信息
public static Mapgetcitydetailbyjingwei(double jing ,double wei) { Map map = null; String url = "http://api.map.baidu.com/reverse_geocoding/v3/?" + "ak=你的AK&output=json&coordtype=wgs84ll&location=" +wei+","+jing; try { HttpClient client = HttpClientBuilder.create().build();//构建一个Client HttpGet get = new HttpGet(url.toString()); //构建一个GET请求 HttpResponse response = client.execute(get);//提交GET请求 HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体" String content = EntityUtils.toString(result); JSONObject res = JSONObject.fromObject(content); map = JsonUtil.parseJSON2Map(res); //通过下面的函数将json转化为map } catch (Exception e) { e.printStackTrace(); System.out.println("获取地址失败"); } return map; }
通过下面的方法将JSON转化为Map
import net.sf.json.JSONArray;import net.sf.json.JSONObject;import java.util.*;public class JsonUtil { public static MapparseJSON2Map(JSONObject json) { Map map = new HashMap (); // 最外层解析 for (Object k : json.keySet()) { Object v = json.get(k); // 如果内层还是数组的话,继续解析 if (v instanceof JSONArray) { List
输入经度和纬度:118.46566 36.55001
得到的结果
转载地址:http://xnzci.baihongyu.com/