wlzboy
19 小时以前 10354e63eb3298beb9ebcc029dd9f48d8936a272
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TiandituMapServiceImpl.java
@@ -1,6 +1,7 @@
package com.ruoyi.system.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.config.TiandituMapConfig;
import com.ruoyi.system.service.IMapService;
@@ -40,7 +41,14 @@
        if (address == null || address.trim().isEmpty()) {
            return null;
        }
        if(address.indexOf(",")>0){
            String[] split = address.split(",");
            address=split[0];
        }
        if(address.indexOf(",")>0){
            String[] split = address.split(",");
            address=split[0];
        }
        try {
            // 构建天地图地理编码API URL
            String url = "http://api.tianditu.gov.cn/geocoder";
@@ -62,14 +70,9 @@
                return null;
            }
            
            // 提取坐标
            JSONObject result = jsonObject.getJSONObject("result");
            if (result == null) {
                logger.warn("天地图地理编码响应无result: address={}", address);
                return null;
            }
            
            JSONObject location = result.getJSONObject("location");
            JSONObject location = jsonObject.getJSONObject("location");
            if (location == null) {
                logger.warn("天地图地理编码响应无location: address={}", address);
                return null;
@@ -91,4 +94,39 @@
            return null;
        }
    }
    /**
     * 直接返回地址
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    @Override
    public String reverseGeocoding(double lng, double lat) {
        // 构建天地图逆地理编码API URL
        String url = "http://api.tianditu.gov.cn/geocoder";
        String params = "postStr={\"lon\":" + lng + ",\"lat\":" + lat + ",\"ver\":1}" +
                "&type=geocode" +
                "&tk=" + tiandituMapConfig.getTk();
        logger.info("天地图逆地理编码请求: lon={}, lat={}", lng, lat);
        // 发送HTTP请求
        String response = HttpUtils.sendGet(url, params);
        // "{\"msg\":\"ok\",\"location\":{\"score\":100,\"level\":\"门址\",\"lon\":\"116.290158\",\"lat\":\"39.894696\",\"keyWord\":\"北京市海淀区莲花池西路28号\"},\"searchVersion\":\"7.4.3V\",\"status\":\"0\"}"
        logger.info("天地图逆地理编码响应: {}", response);
        com.alibaba.fastjson.JSONObject obj= com.alibaba.fastjson.JSONObject.parseObject(response);
        if (obj.getInteger("status") !=0) {
            logger.error("天地图逆地理编码失败: {}", response);
            return null;
        }
        com.alibaba.fastjson.JSONObject location = obj.getJSONObject("result");
        if (location == null) {
            logger.error("天地图逆地理编码响应无location: {}", response);
            return null;
        }
        return location.getString("formatted_address");
    }
}