| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.core.utils.Tencent; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.core.utils.Http; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.URLEncoder; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * è
¾è®¯å°å¾å·¥å
·ç±» |
| | | * |
| | | * @Author : Rk |
| | | * @create 2026/4/14 15:58 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class MapUtil { |
| | | |
| | | private static String tencentKey; |
| | | |
| | | /** è·ç¦»ç©éµAPI */ |
| | | public static final String MATRIX_URL = "https://apis.map.qq.com/ws/distance/v1/matrix"; |
| | | |
| | | /** æ¯æçæ¨¡å¼ */ |
| | | private static final List<String> SUPPORTED_MODES = Arrays.asList("driving", "bicycling"); |
| | | |
| | | @Value("${tencent_key}") |
| | | public void setTencentKey(String tencentKey) { |
| | | MapUtil.tencentKey = tencentKey; |
| | | } |
| | | |
| | | /** |
| | | * æ¹éè·ç¦»ç©éµè®¡ç® |
| | | * |
| | | * @param mode 模å¼ï¼driving(驾车)ãbicycling(èªè¡è½¦) |
| | | * @param fromPoints èµ·ç¹åè¡¨ï¼æ ¼å¼ï¼lat,lngï¼æå¤10ä¸ªï¼ |
| | | * @param toPoints ç»ç¹åè¡¨ï¼æ ¼å¼ï¼lat,lngï¼æå¤20ä¸ªï¼ |
| | | * @return result.rows ç©éµæ°æ®ï¼æ¯ä¸ªå
ç´ å
å« distance(ç±³) å duration(ç§) |
| | | */ |
| | | public static JSONObject distanceMatrix(String mode, List<String> fromPoints, List<String> toPoints) { |
| | | if (!SUPPORTED_MODES.contains(mode)) { |
| | | throw new IllegalArgumentException("䏿¯æç模å¼: " + mode + "ï¼ä»
æ¯æ: " + SUPPORTED_MODES); |
| | | } |
| | | if (fromPoints == null || fromPoints.isEmpty() || toPoints == null || toPoints.isEmpty()) { |
| | | throw new IllegalArgumentException("èµ·ç¹åç»ç¹å表ä¸è½ä¸ºç©º"); |
| | | } |
| | | |
| | | String from = String.join(";", fromPoints); |
| | | String to = String.join(";", toPoints); |
| | | |
| | | try { |
| | | String url = MATRIX_URL |
| | | + "?key=" + tencentKey |
| | | + "&mode=" + mode |
| | | + "&from=" + URLEncoder.encode(from, "UTF-8") |
| | | + "&to=" + URLEncoder.encode(to, "UTF-8"); |
| | | |
| | | log.info("è
¾è®¯å°å¾ç©éµAPI请æ±: mode={}, from={}, to={}", mode, from, to); |
| | | |
| | | JSONObject json = new Http().build(url) |
| | | .setConnectTimeout(5000) |
| | | .setReadTimeout(10000) |
| | | .get() |
| | | .toJSONObject(); |
| | | |
| | | log.info("è
¾è®¯å°å¾ç©éµAPIååº: {}", json); |
| | | |
| | | if (json.getIntValue("status") != 0) { |
| | | throw new RuntimeException("è
¾è®¯å°å¾ç©éµAPIè°ç¨å¤±è´¥: " + json.getString("message")); |
| | | } |
| | | |
| | | return json.getJSONObject("result"); |
| | | } catch (IOException e) { |
| | | log.error("è
¾è®¯å°å¾ç©éµAPIè°ç¨å¼å¸¸", e); |
| | | throw new RuntimeException("è
¾è®¯å°å¾ç©éµAPIè°ç¨å¼å¸¸", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å对è·ç¦»è®¡ç®ï¼ä¾¿æ·æ¹æ³ï¼ |
| | | * |
| | | * @param mode 模å¼ï¼driving(驾车)ãbicycling(èªè¡è½¦) |
| | | * @param from èµ·ç¹æ ¼å¼ï¼lat,lng |
| | | * @param to ç»ç¹æ ¼å¼ï¼lat,lng |
| | | * @return 第ä¸ä¸ªå
ç´ ç distance(ç±³) å duration(ç§) |
| | | */ |
| | | public static JSONObject distanceSingle(String mode, String from, String to) { |
| | | JSONObject result = distanceMatrix(mode, |
| | | Arrays.asList(from), |
| | | Arrays.asList(to)); |
| | | JSONArray rows = result.getJSONArray("rows"); |
| | | if (rows != null && !rows.isEmpty()) { |
| | | JSONArray elements = rows.getJSONObject(0).getJSONArray("elements"); |
| | | if (elements != null && !elements.isEmpty()) { |
| | | return elements.getJSONObject(0); |
| | | } |
| | | } |
| | | return new JSONObject(); |
| | | } |
| | | |
| | | /** |
| | | * å¤èµ·ç¹å°åç»ç¹ï¼ä¾¿æ·æ¹æ³ï¼ |
| | | * è¿åæ¯ä¸ªèµ·ç¹å°ç»ç¹çè·ç¦»åèæ¶ï¼é¡ºåºä¸fromPointså¯¹åº |
| | | * |
| | | * @param mode æ¨¡å¼ |
| | | * @param fromPoints èµ·ç¹å表 |
| | | * @param to å个ç»ç¹ |
| | | * @return è·ç¦»èæ¶å表ï¼é¡ºåºä¸fromPointså¯¹åº |
| | | */ |
| | | public static List<JSONObject> distanceToOne(String mode, List<String> fromPoints, String to) { |
| | | JSONObject result = distanceMatrix(mode, fromPoints, Arrays.asList(to)); |
| | | JSONArray rows = result.getJSONArray("rows"); |
| | | return rows == null ? Arrays.asList() : rows.stream() |
| | | .map(row -> ((JSONObject) row).getJSONArray("elements").getJSONObject(0)) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | } |