使用腾讯地图API根据详细地址反查坐标经纬度

业务背景:地图数据需要某个地点的详细坐标,通过调研,腾讯地图的API较为开放,个人每日10000次,并发5次/秒,企业1000000万次,基本符合业务需求。

首先需要再腾讯地图开通个人或者企业账号,会给开通一个key,每次访问接口都需要带这个key

public void getCoordinate(String address) {
        String requestUrl="https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"&key="+qqMapKey;
        String response = HttpClientUtil.sendGetRequest(requestUrl, "UTF-8");
        log.info("腾讯地图地址:{},查询坐标响应内容:{}",address,response);
        QqMapResponseDto responseDto = JSONObject.parseObject(response, QqMapResponseDto.class);
        if(responseDto.getStatus() == 0){
            LocationDto location = responseDto.getResult().getLocation();
            return JSONObject.toJSONString(location)
        }
@Data
public class QqMapResponseDto {
    private Integer status;
    private String message;
    private QqMapResultDto result;
}
@Data
public class LocationDto {
    private BigDecimal lng;
    private BigDecimal lat;
}
public static String sendGetRequest(String reqURL, String decodeCharset){
        log.info("get请求:start---------------------");
        long responseLength = 0;
        String responseContent = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(reqURL);
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpGet.setConfig(config);
        try {
            // 执行get请求
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if (null != entity) {
                    responseLength = entity.getContentLength();
                    responseContent = EntityUtils.toString(entity, decodeCharset == null ? "UTF-8" : decodeCharset);
                    EntityUtils.consume(entity);
                }
            }
        } catch (Exception e) {
            log.error("与[" + reqURL + "]通信过程中发生异常,堆栈信息如下:{}", e);
        }finally {
            try {
                httpClient.close();
            }catch (IOException e){
                log.error("关闭[+"+reqURL+"]连接发生异常,堆栈信息:{}",e);
            }

        }
        log.debug("请求地址:{}", reqURL);
        log.debug("响应长度:{}", responseLength);
        log.debug("响应内容:{}", responseContent);
        log.debug("get请求:end---------------------");
        return responseContent;
    }

该API后续监控到腾讯对于地图数据查询会不断自动完善功能,本公司业务 前期对于查询不到的数据 进行手动调接口维护,后续发现腾讯地图对于该地址数据进行了补充。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力终会有回报

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值