使用高德API根据经纬度获取一定范围内的地址信息
PS:
1、高德地图接口Key申请步骤:https://zhuanlan.zhihu.com/p/555130433
2、经纬度在线查询网址:https://map.jiqrxx.com/jingweidu/
3、高德地图API资料地址:https://lbs.amap.com/api/webservice/guide/api/georegeo
4、高德地图响应错误码文档地址:https://lbs.amap.com/api/webservice/guide/tools/info
/**
*根据经纬度获取省市区
*/
public String getAddressByJWD(String clog, String clat){
//lat 小 log 大
//参数解释: 纬度,经度 采用高德API可参考高德文档https://lbs.amap.com/
//注意key是在高德开放平台申请的key,具体获得key的步骤请查看网址:https://developer.amap.com/api/webservice/guide/create-project/get-key
String key = gaode_key;
String parameters="?key="+key;
parameters+="&location="+clog+","+clat;//经纬度坐标
parameters+="&extensions=all";//返回结果控制,extensions 参数取值为 all 时会返回基本地址信息、附近 POI 内容、道路信息以及道路交叉口信息。
parameters+="&batch=false";//批量查询控制,batch 参数设置为 false 时进行单点查询,此时即使传入多个经纬度也只返回第一个经纬度的地址解析查询结果。
parameters+="&roadlevel=0";//道路等级,当 roadlevel = 0 时,显示所有道路
parameters+="&output=JSON";
//parameters+="&radius=300";//搜索半径,radius取值范围在0~3000,默认是1000。单位:米
String urlString = "https://restapi.amap.com/v3/geocode/regeo"+parameters;
StringBuilder res = new StringBuilder();
try {
log.info("高德地图请求地址:"+urlString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
res.append(line);
}
in.close();
//解析结果
log.info("高德返回结果:" + res);
//com.alibaba.fastjson.JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
//res = new StringBuilder(jsonObject1.getString("formatted_address"));
} catch (Exception e) {
log.info("获取地址信息异常");
e.printStackTrace();
return null;
}
return res.toString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)