puzhibing
2026-03-14 f572d49e6f1c20394bbb26d123cd565d70012633
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
106
107
108
109
110
111
112
113
114
115
package com.xiaopeng.map.utils;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
 
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
 
public class GaodeMapService {
    
    // 高德地图API基础URL
    private static final String WALKING_API_URL = "https://restapi.amap.com/v3/direction/walking";
    
    // 你的高德地图API Key
//    @Value("${map.apikey}")
    private String apiKey="66ae577f63898dae3016d599fda57221";
    
 
    /**
     * 获取步行路径规划
     * @param origin 起点坐标,格式:经度,纬度
     * @param destination 终点坐标,格式:经度,纬度
     * @return 步行路线结果
     */
    public AmapWalkingRouteUtil.WalkingRouteResult getWalkingRoute(String origin, String destination) {
        AmapWalkingRouteUtil.WalkingRouteResult result = new AmapWalkingRouteUtil.WalkingRouteResult();
        result.setOrigin(origin);
        result.setDestination(destination);
        
        try {
            // 构建请求URL
            String url = buildWalkingUrl(origin, destination);
            
            // 发送HTTP请求
            String responseJson = sendHttpGetRequest(url);
            
            // 解析响应
            return parseWalkingResponse(responseJson, result);
            
        } catch (Exception e) {
            result.setSuccess(false);
            return result;
        }
    }
    
    /**
     * 构建步行路径规划URL
     */
    private String buildWalkingUrl(String origin, String destination) {
        return String.format("%s?origin=%s&destination=%s&key=%s",
                WALKING_API_URL,
                origin,
                destination,
                apiKey);
    }
    
    /**
     * 发送HTTP GET请求
     */
    private String sendHttpGetRequest(String url) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity, StandardCharsets.UTF_8);
                }
            }
        }
        return null;
    }
    
    /**
     * 解析步行响应结果
     */
    private AmapWalkingRouteUtil.WalkingRouteResult parseWalkingResponse(String responseJson, AmapWalkingRouteUtil.WalkingRouteResult result) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(responseJson);
        
        // 检查状态码
        String status = rootNode.path("status").asText();
        if (!"1".equals(status)) {
            result.setSuccess(false);
            return result;
        }
        
        // 解析路径信息
        JsonNode routeNode = rootNode.path("route");
        JsonNode pathsNode = routeNode.path("paths");
        
        if (pathsNode.isArray() && pathsNode.size() > 0) {
            JsonNode firstPath = pathsNode.get(0);
            
            // 获取距离和时间
            int distance = firstPath.path("distance").asInt();
            int duration = firstPath.path("duration").asInt();
            
            result.setDistance(distance);
            result.setDuration(duration);
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
        }
        
        return result;
    }
}