wlzboy
1 天以前 08f95b2f159b56fa3bd4f4b348855989de8aa456
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.ruoyi.system.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.config.BaiduMapConfig;
import com.ruoyi.system.service.IMapService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 百度地图服务实现
 * 
 * @author ruoyi
 */
@Service("baiduMapService")
public class BaiduMapServiceImpl implements IMapService {
    
    private static final Logger logger = LoggerFactory.getLogger(BaiduMapServiceImpl.class);
    
    @Autowired
    private BaiduMapConfig baiduMapConfig;
    
    /**
     * 地址转GPS坐标(地理编码)
     * 
     * @param address 地址
     * @param city 城市(可选,用于提高解析准确性)
     * @return GPS坐标,包含lng和lat,如果获取失败返回null
     */
    @Override
    public Map<String, Double> geocoding(String address, String city) {
        // 如果地址为空,直接返回null
        if (address == null || address.trim().isEmpty()) {
            return null;
        }
        
        try {
            // 构建百度地图地理编码API URL
            String url = "https://api.map.baidu.com/geocoding/v3/";
            String params = "address=" + URLEncoder.encode(address, StandardCharsets.UTF_8.toString()) +
                           (city != null && !city.trim().isEmpty() ? 
                            "&city=" + URLEncoder.encode(city, StandardCharsets.UTF_8.toString()) : "") +
                           "&output=json" +
                           "&ak=" + baiduMapConfig.getAk();
            
            logger.info("百度地图地理编码请求: address={}, city={}", address, city);
            
            // 发送HTTP请求
            String response = HttpUtils.sendGet(url, params);
            
            // 解析响应
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject.getInteger("status") != 0) {
                logger.warn("百度地图地理编码失败: address={}, status={}, message={}", 
                           address, jsonObject.getInteger("status"), jsonObject.getString("message"));
                return null;
            }
            
            // 提取坐标
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            double lng = location.getDouble("lng");
            double lat = location.getDouble("lat");
            
            logger.info("百度地图地理编码成功: address={}, lng={}, lat={}", address, lng, lat);
            
            Map<String, Double> coordinates = new HashMap<>();
            coordinates.put("lng", lng);
            coordinates.put("lat", lat);
            
            return coordinates;
        } catch (Exception e) {
            // 捕获所有异常,避免影响主流程
            logger.error("百度地图地理编码异常: address=" + address, e);
            return null;
        }
    }
 
    @Override
    public String reverseGeocoding(double lng, double lat) {
        //调用百度地址反向解析接口
        String url = "https://api.map.baidu.com/reverse_geocoding/v3/";
        String params = "location=" + lat + "," + lng +
                "&output=json" +
                "&ak=" + baiduMapConfig.getAk();
        String response = HttpUtils.sendGet(url, params);
        //"{\"status\":0,\"result\":{\"location\":{\"lng\":113.25330399999999,\"lat\":23.04668300960378},\"formatted_address\":\"广东省佛山市南海区桂城街道\",\"edz\":{\"name\":\"\"},\"business\":\"\",\"business_info\":[],\"addressComponent\":{\"country\":\"中国\",\"country_code\":0,\"region_code_iso\":\"CHN\",\"country_code_iso\":\"CHN\",\"country_code_iso2\":\"CN\",\"province\":\"广东省\",\"city\":\"佛山市\",\"city_level\":2,\"district\":\"南海区\",\"town\":\"桂城街道\",\"town_code\":\"440605011\",\"distance\":\"\",\"direction\":\"\",\"adcode\":\"440605\",\"street\":\"\",\"street_number\":\"\"},\"pois\":[],\"roads\":[],\"poiRegions\":[],\"sematic_description\":\"\",\"formatted_address_poi\":\"\",\"cityCode\":138}}"
        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getInteger("status") != 0) {
            logger.warn("百度地图地址反向解析失败: lng={}, lat={}, status={}, message={}",
                    lng, lat, jsonObject.getInteger("status"), jsonObject.getString("message"));
            return null;
        }
        String address = jsonObject.getJSONObject("result").getString("formatted_address");
        logger.info("百度地图地址反向解析成功: lng={}, lat={}, address={}", lng, lat, address);
        return address;
    }
}