通过天地图来对接
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxskz.zangyushangcheng.pojo.vo.LngAndLatVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Classname name = AddressUtil
* @Description TODO
* @Version 1.0.0
* @Date 2022/9/26 9:49
* @Created by ${Mr.Yao}
*/
public class AddressUtil {
private static final Logger log = LoggerFactory.getLogger(AddressUtil.class);
//参数 : 地址
public static LngAndLatVO getLatAndLngByAddr(String addr) {
try {
String key = "自己的Key";
String result = "";
addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" + key;
/**
*HttpClientUtil获取路径:
*https://blog.csdn.net/weixin_40052298/article/details/123347111?spm=1001.2014.3001.5501
*/
String info = HttpClientUtil.get(queryStr);
if (StringUtils.isNotBlank(info)) {
JSONObject resultJson = JSON.parseObject(info);
if (!"无结果".equals(resultJson.get("msg"))) {
JSONObject locationObj = (JSONObject) resultJson.get("location");
//纬度
String lat = locationObj.get("lat") + "";
//经度
String lng = locationObj.get("lon") + "";
LngAndLatVO lngAndLatVO = new LngAndLatVO();
lngAndLatVO.setLng(lng);
lngAndLatVO.setLat(lat);
return lngAndLatVO;
}
}
return null;
} catch (Exception e) {
return null;
}
}
}