java调用天地图API根据具体地址获取经纬度工具类
天地图注册地址,创建应用获取key,为何用天地图天地图免费不限次数
public class AddressUtil {
private static final Logger log = LoggerFactory.getLogger(AddressUtil.class);
public static String getLatAndLngByAddr(String addr) {
try {
addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" +
"在天地图申请的key";
String info = HttpClientUtil.doGet(queryStr);
//Thread.sleep(10);
if (StringUtils.isNotBlank(info)) {
JSONObject resultJson = JSON.parseObject(info);
log.error(">>>>>>>>>>>" + info + ">>>>>>>>>>>");
if (!"无结果".equals(resultJson.get("msg"))) {
JSONObject locationObj = (JSONObject) resultJson.get("location");
//纬度
String lat = locationObj.get("lat") + "";
//经度
String lng = locationObj.get("lon") + "";
StringBuffer sbf = new StringBuffer();
sbf.append(lng).append(",").append(lat);
return sbf.toString();
}
}
return "无";
} catch (Exception e) {
}
return "无";
}
public static void main(String[] args) {
System.out.println(getLatAndLngByAddr("中国上海闵行区元江路5500号第1幢C379室"));
}
}
来源:CSDN
作者:SZ_酸萝卜
链接:https://blog.csdn.net/B_Courier/article/details/103990408