puzhibing
2026-05-17 993a6e6ea8ae047b9e5fffd249ae7adfcd650d3a
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package com.xiaopeng.map.utils;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.vo.map.PointByIpVo;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.HttpUtils;
import com.xiaopeng.map.constant.I18nKeyConstant;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
/**
 * 高德地图步行导航工具类
 */
@Slf4j
public class AmapWalkingRouteUtil {
 
    // 高德地图Web服务API配置
    private static final String AMAP_API_BASE_URL = "https://restapi.amap.com/v3/direction/walking";
    private static final String AMAP_GEOCODING_URL = "https://restapi.amap.com/v3/geocode/geo";
    // 高德API基础URL
    private static final String IP_API_URL = "https://restapi.amap.com/v3/ip";
    private static final String GEOCODE_API_URL = "https://restapi.amap.com/v3/geocode/geo";
 
 
    // HTTP客户端
    private final OkHttpClient httpClient;
    private final ObjectMapper objectMapper;
 
 
    //    @Value("${map.apikey}")
    private static String apiKey="66ae577f63898dae3016d599fda57221";
 
    /**
     * IP定位
     */
    public static PointByIpVo ipLocation(String ip) throws IOException {
        PointByIpVo pointByIpVo = new PointByIpVo();
        StringBuilder urlBuilder = new StringBuilder(IP_API_URL)
                .append("?key=").append(apiKey)
                .append("&output=json");
 
        if (ip != null && !ip.trim().isEmpty()) {
            urlBuilder.append("&ip=").append(ip);
        }
 
        String response = HttpUtils.sendGet(urlBuilder.toString());
        JSONObject jsonObject = JSONObject.parseObject(response);
 
        // 城市名称
        String city = jsonObject.getString("city");
        String province = jsonObject.getString("province");
        if(("局域网").equals(province)){
            pointByIpVo.setCity("成都市");
            LonLatDto lonLatDto = new LonLatDto(104.07, 30.660);
            pointByIpVo.setPoint(lonLatDto);
            pointByIpVo.setAdcode("510100");
            return pointByIpVo;
        }
        String adcode = jsonObject.getString("adcode");
        if (city == null || city.trim().isEmpty() || "[]".equals(city)) {
            // 直辖市:adcode前两位是11,12,31,50
            if (adcode != null) {
                String prefix = adcode.substring(0, 2);
                if ("11".equals(prefix)) {
                    city = "北京市";
                }
                if ("12".equals(prefix)) {
                    city = "天津市";
                }
                if ("31".equals(prefix)) {
                    city = "上海市";
                }
                if ("50".equals(prefix)) {
                    city = "重庆市";
                }
                // 如果没有city但有province,使用省份
                city = province;
            }
        }
 
        LonLatDto lonLatDto = geocodeCity(city);
        pointByIpVo.setCity(city);
        pointByIpVo.setPoint(lonLatDto);
        pointByIpVo.setAdcode(adcode);
        return pointByIpVo;
 
    }
 
    private static LonLatDto geocodeCity(String cityName) throws IOException {
        String url = GEOCODE_API_URL + "?key=" +apiKey + "&address=" + cityName + "&output=json&city=" + cityName;
        String response = HttpUtils.sendGet(url);
        System.out.println(response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        // 获取geocodes数组
        JSONArray geocodes = jsonObject.getJSONArray("geocodes");
        if (geocodes == null || geocodes.isEmpty()) {
            return null;
        }
 
        // 取第一个geocode
        JSONObject firstGeocode = geocodes.getJSONObject(0);
 
        // 获取location字段
        String lonLat = firstGeocode.getString("location");
        String[] lonLatArray = lonLat.split(",");
        double longitude = Double.parseDouble(lonLatArray[0]);
        double latitude = Double.parseDouble(lonLatArray[1]);
        return new LonLatDto(longitude, latitude);
 
    }
        public static void main (String[]args) throws IOException {
            geocodeCity("凉山彝族自治州");
 
        }
 
 
        /**
         * 构造函数
         * @param apiKey 高德地图API Key
         */
    public AmapWalkingRouteUtil(String apiKey) {
            this.apiKey = apiKey;
            this.httpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            this.objectMapper = new ObjectMapper();
        }
 
        /**
         * 位置坐标类
         */
        public static class Location {
            private final double longitude; // 经度
            private final double latitude;  // 纬度
 
            public Location(double longitude, double latitude) {
                this.longitude = longitude;
                this.latitude = latitude;
            }
 
            public double getLongitude() {
                return longitude;
            }
 
            public double getLatitude() {
                return latitude;
            }
 
            @Override
            public String toString() {
                return String.format("%.6f,%.6f", longitude, latitude);
            }
        }
 
        /**
         * 步行路线结果类
         */
        public static class WalkingRouteResult {
            private int duration;      // 步行时间(秒)
            private int distance;      // 步行距离(米)
            private String origin;     // 起点地址
            private String destination; // 终点地址
            private boolean success;   // 是否成功
            private String message;    // 返回信息
 
            // getters and setters
            public int getDuration() {
                return duration;
            }
 
            public void setDuration(int duration) {
                this.duration = duration;
            }
 
            public int getDistance() {
                return distance;
            }
 
            public void setDistance(int distance) {
                this.distance = distance;
            }
 
            public String getOrigin() {
                return origin;
            }
 
            public void setOrigin(String origin) {
                this.origin = origin;
            }
 
            public String getDestination() {
                return destination;
            }
 
            public void setDestination(String destination) {
                this.destination = destination;
            }
 
            public boolean isSuccess() {
                return success;
            }
 
            public void setSuccess(boolean success) {
                this.success = success;
            }
 
            public String getMessage() {
                return message;
            }
 
            public void setMessage(String message) {
                this.message = message;
            }
 
            /**
             * 获取格式化的步行时间
             * @return 格式化的时间字符串(如:15分钟30秒)
             */
            public String getFormattedDuration() {
 
                int seconds = duration;
                int minutes = seconds / 60;
                seconds = seconds % 60;
                return String.format("%d分%d秒", minutes, seconds);
            }
 
            /**
             * 获取格式化的步行距离
             * @return 格式化的距离字符串(如:1.2公里)
             */
            public String getFormattedDistance() {
                if (distance >= 1000) {
                    double km = distance / 1000.0;
                    return String.format("%.1f公里", km);
                } else {
                    return String.format("%d米", distance);
                }
            }
        }
 
        /**
         * 根据地址获取经纬度坐标
         * @param address 地址信息
         * @return 经纬度坐标
         */
        public Location getLocationByAddress (String address) throws IOException {
            Map<String, String> params = new HashMap<>();
            params.put("key", apiKey);
            params.put("address", address);
 
            String url = buildUrl(AMAP_GEOCODING_URL, params);
            Request request = new Request.Builder().url(url).build();
 
            try (Response response = httpClient.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    throw new IOException("请求失败: " + response);
                }
 
                String responseBody = response.body().string();
                JsonNode rootNode = objectMapper.readTree(responseBody);
 
                if ("1".equals(rootNode.get("status").asText())) {
                    JsonNode geocodes = rootNode.get("geocodes");
                    if (geocodes != null && geocodes.size() > 0) {
                        String locationStr = geocodes.get(0).get("location").asText();
                        String[] locationArray = locationStr.split(",");
                        double longitude = Double.parseDouble(locationArray[0]);
                        double latitude = Double.parseDouble(locationArray[1]);
                        return new Location(longitude, latitude);
                    }
                }
 
                throw new IOException("地址解析失败: " + rootNode.get("info").asText());
            }
        }
 
        /**
         * 获取步行路线和时间
         * @param origin 起点坐标
         * @param destination 终点坐标
         * @return 步行路线结果
         */
        public WalkingRouteResult getWalkingRoute (Location origin, Location destination){
            WalkingRouteResult result = new WalkingRouteResult();
 
            try {
                Map<String, String> params = new HashMap<>();
                params.put("key", apiKey);
                params.put("origin", origin.toString());
                params.put("destination", destination.toString());
 
                String url = buildUrl(AMAP_API_BASE_URL, params);
                Request request = new Request.Builder().url(url).build();
 
                try (Response response = httpClient.newCall(request).execute()) {
                    if (!response.isSuccessful()) {
                        result.setSuccess(false);
                        result.setMessage("HTTP请求失败: " + response.code());
                        return result;
                    }
 
                    String responseBody = response.body().string();
                    JsonNode rootNode = objectMapper.readTree(responseBody);
 
                    String status = rootNode.get("status").asText();
                    String info = rootNode.get("info").asText();
 
                    if ("1".equals(status)) {
                        JsonNode route = rootNode.get("route");
                        JsonNode paths = route.get("paths");
 
                        if (paths != null && paths.size() > 0) {
                            JsonNode firstPath = paths.get(0);
                            int duration = firstPath.get("duration").asInt();
                            int distance = firstPath.get("distance").asInt();
 
                            result.setDuration(duration);
                            result.setDistance(distance);
                            result.setSuccess(true);
                            result.setMessage("成功获取步行路线");
 
                            // 尝试获取地址信息
                            result.setOrigin(getAddressByLocation(origin));
                            result.setDestination(getAddressByLocation(destination));
 
                        } else {
                            result.setSuccess(false);
                            result.setMessage("未找到步行路线");
                            log.info("未找到步行路线");
                            throw new ServiceException("未找到步行路线", I18nKeyConstant.NO_WALKING_ROUTE_FOUND);
                        }
                    } else {
                        result.setSuccess(false);
                        result.setMessage("API返回错误: " + info);
                        log.info("API返回错误" + info);
                        throw new ServiceException("未找到步行路线", I18nKeyConstant.NO_WALKING_ROUTE_FOUND);
                    }
                }
            } catch (Exception e) {
                result.setSuccess(false);
                result.setMessage("获取步行路线失败: " + e.getMessage());
                log.info("获取步行路线失败" + e.getMessage());
                throw new ServiceException("未找到步行路线", I18nKeyConstant.NO_WALKING_ROUTE_FOUND);
            }
 
            return result;
        }
 
        /**
         * 根据经纬度获取地址(逆地理编码)
         * @param location 经纬度坐标
         * @return 地址信息
         */
        private String getAddressByLocation (Location location){
            try {
                Map<String, String> params = new HashMap<>();
                params.put("key", apiKey);
                params.put("location", location.toString());
 
                String url = buildUrl("https://restapi.amap.com/v3/geocode/regeo", params);
                Request request = new Request.Builder().url(url).build();
 
                try (Response response = httpClient.newCall(request).execute()) {
                    if (response.isSuccessful()) {
                        String responseBody = response.body().string();
                        JsonNode rootNode = objectMapper.readTree(responseBody);
 
                        if ("1".equals(rootNode.get("status").asText())) {
                            JsonNode regeocode = rootNode.get("regeocode");
                            if (regeocode != null) {
                                return regeocode.get("formatted_address").asText();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                // 地址解析失败时返回坐标
            }
            return location.toString();
        }
 
        /**
         * 构建带参数的URL
         * @param baseUrl 基础URL
         * @param params 参数Map
         * @return 完整的URL
         */
        private String buildUrl (String baseUrl, Map < String, String > params){
            StringBuilder urlBuilder = new StringBuilder(baseUrl).append("?");
            params.forEach((key, value) -> {
                urlBuilder.append(key).append("=").append(value).append("&");
            });
            // 删除最后一个&
            urlBuilder.deleteCharAt(urlBuilder.length() - 1);
            return urlBuilder.toString();
        }
 
        /**
         * 计算两点间的直线距离(仅供参考,不是实际步行距离)
         * @param loc1 坐标1
         * @param loc2 坐标2
         * @return 直线距离(米)
         */
        public static double calculateStraightDistance (Location loc1, Location loc2){
            double earthRadius = 6371000; // 地球半径(米)
            double lat1 = Math.toRadians(loc1.getLatitude());
            double lat2 = Math.toRadians(loc2.getLatitude());
            double deltaLat = Math.toRadians(loc2.getLatitude() - loc1.getLatitude());
            double deltaLon = Math.toRadians(loc2.getLongitude() - loc1.getLongitude());
 
            double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
                    Math.cos(lat1) * Math.cos(lat2) *
                            Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
 
            return earthRadius * c;
        }
 
 
    }