From 3aef471b170a703b501ddb4d9d2a12791d07ff28 Mon Sep 17 00:00:00 2001
From: rk <94314517@qq.com>
Date: 星期三, 15 四月 2026 20:06:56 +0800
Subject: [PATCH] 代码生成
---
server/services/src/main/java/com/doumee/core/utils/Tencent/MapUtil.java | 128 +++++++++++++++++++++++++++++++++++++++++-
1 files changed, 123 insertions(+), 5 deletions(-)
diff --git a/server/services/src/main/java/com/doumee/core/utils/Tencent/MapUtil.java b/server/services/src/main/java/com/doumee/core/utils/Tencent/MapUtil.java
index 0d78ce3..cf377b8 100644
--- a/server/services/src/main/java/com/doumee/core/utils/Tencent/MapUtil.java
+++ b/server/services/src/main/java/com/doumee/core/utils/Tencent/MapUtil.java
@@ -1,6 +1,124 @@
-package com.doumee.core.utils.Tencent;/**
-* Created by IntelliJ IDEA.
-* @Author : Rk
-* @create 2026/4/14 15:58
-*/public class MapUtil {
+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 妯″紡锛歞riving(椹捐溅)銆乥icycling(鑷杞�)
+ * @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 妯″紡锛歞riving(椹捐溅)銆乥icycling(鑷杞�)
+ * @param from 璧风偣鏍煎紡锛歭at,lng
+ * @param to 缁堢偣鏍煎紡锛歭at,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());
+ }
}
--
Gitblit v1.9.3