JAVA后端获取经纬度

通过天地图来对接

国家地理信息公共服务平台 天地图

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;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值