复制粘贴即用,简单,明了!!!!!
自己淋过雨,所以想为他人撑伞,各位小伙伴,一起加油!!!
Java实现根据地址调用高德地图获取经纬度等信息
近期忙碌的工作暂时告一段落,接下来发布一篇根据地址信息调用高德API 获取经纬度等信息的具体代码实现,附效果图
先附上代码截图,完整代码在图片下方
第一步 申请 key
申请高德地图的key 这是必须的!前面的文章提到过,如果你是公司使用,那不用替公司省钱,申请企业版的key
第二步 有需要的直接使用
public static JSONObject getLngAndLat(String address, String key) {
JSONObject positionObj = new JSONObject();
try {
// 拼接请求高德的url
String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address.trim() + "&output=JSON&key=" + key;
// 请求高德接口
String result = HttpClient.sendHttpGet(url);
JSONObject resultJOSN = JSONObject.parseObject(result);
System.out.println("高德接口返回原始数据:");
System.out.println(resultJOSN);
JSONArray geocodesArray = resultJOSN.getJSONArray("geocodes");
if (geocodesArray.size() > 0) {
String position = geocodesArray.getJSONObject(0).getString("location");
String[] lngAndLat = position.split(",");
String longitude = lngAndLat[0];
String latitude = lngAndLat[1];
// 这里的两个值就是经纬度
positionObj.put("longitude", longitude);
positionObj.put("latitude", latitude);
}
geocodesArray.getJSONObject(0).getString("location");
} catch (Exception e) {
e.printStackTrace();
}
return positionObj;
}
第三步 测试调用
public static void main(String[] args) {
System.out.println(getLngAndLat("北京市朝阳区阜通东大街6号","这里是你申请的key"));
}
输出结果
高德接口返回原始数据:
{“count”:“1”,“geocodes”:[{“adcode”:“110105”,“building”:{“name”:[],“type”:[]},“city”:“北京市”,“citycode”:“010”,“country”:“中国”,“district”:“朝阳区”,“formatted_address”:“北京市朝阳区阜通东大街6号”,“level”:“门牌号”,“location”:“116.482086,39.990496”,“neighborhood”:{“name”:[],“type”:[]},“number”:“6号”,“province”:“北京市”,“street”:“阜通东大街”,“township”:[]}],“info”:“OK”,“infocode”:“10000”,“status”:“1”}
{“latitude”:“39.990496”,“longitude”:“116.482086”}