From eb7a808aaf7dd0a6dd2ff70f9ef3f8ce0b1e31d1 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期五, 22 五月 2026 18:27:32 +0800
Subject: [PATCH] Merge branch 'master' of http://139.186.142.91:10010/r/productDev/gtzxinglijicun
---
server/services/src/main/java/com/doumee/core/utils/geocode/MapUtil.java | 91 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 91 insertions(+), 0 deletions(-)
diff --git a/server/services/src/main/java/com/doumee/core/utils/geocode/MapUtil.java b/server/services/src/main/java/com/doumee/core/utils/geocode/MapUtil.java
index f70ae35..137d704 100644
--- a/server/services/src/main/java/com/doumee/core/utils/geocode/MapUtil.java
+++ b/server/services/src/main/java/com/doumee/core/utils/geocode/MapUtil.java
@@ -1,8 +1,10 @@
package com.doumee.core.utils.geocode;
+import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.utils.Http;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -24,6 +26,9 @@
/** 閫嗗湴鐞嗚В鏋� */
private static final String GEO_URL = "https://restapi.amap.com/v3/geocode/regeo";
+ /** 姝e悜鍦扮悊瑙f瀽 */
+ private static final String GEOCODE_URL = "https://restapi.amap.com/v3/geocode/geo";
+
/** 椹捐溅璺緞瑙勫垝 */
private static final String DRIVING_URL = "https://restapi.amap.com/v3/direction/driving";
@@ -33,6 +38,52 @@
@Value("${geocode_map_key:}")
public void setAmapKey(String amapKey) {
MapUtil.amapKey = amapKey;
+ }
+
+ /**
+ * 姝e悜鍦扮悊瑙f瀽 - 鏍规嵁鍦板潃鑾峰彇缁忕含搴�
+ *
+ * @param address 鍦板潃鏂囨湰锛堝"鍥涘窛鐪佹垚閮藉競"锛�
+ * @return "lat,lng" 鏍煎紡鐨勭粡绾害瀛楃涓诧紝瑙f瀽澶辫触杩斿洖 null
+ */
+ public static String geocode(String address) {
+ try {
+ String url = GEOCODE_URL
+ + "?key=" + amapKey
+ + "&address=" + URLEncoder.encode(address, "UTF-8");
+
+ log.info("楂樺痉鍦板浘姝e悜鍦扮悊瑙f瀽璇锋眰: address={}", address);
+
+ JSONObject json = new Http().build(url)
+ .setConnectTimeout(5000)
+ .setReadTimeout(10000)
+ .get()
+ .toJSONObject();
+
+ log.info("楂樺痉鍦板浘姝e悜鍦扮悊瑙f瀽鍝嶅簲: {}", json);
+
+ if (!"1".equals(json.getString("status"))) {
+ log.warn("楂樺痉鍦板浘姝e悜鍦扮悊瑙f瀽澶辫触: {}", json.getString("info"));
+ return null;
+ }
+
+ JSONArray geocodes = json.getJSONArray("geocodes");
+ if (geocodes == null || geocodes.isEmpty()) {
+ log.warn("楂樺痉鍦板浘姝e悜鍦扮悊瑙f瀽鏃犵粨鏋�: address={}", address);
+ return null;
+ }
+
+ String location = geocodes.getJSONObject(0).getString("location"); // lng,lat
+ if (StringUtils.isBlank(location)) {
+ return null;
+ }
+ String[] parts = location.split(",");
+ // 杞负 lat,lng 鏍煎紡
+ return parts[1] + "," + parts[0];
+ } catch (Exception e) {
+ log.error("楂樺痉鍦板浘姝e悜鍦扮悊瑙f瀽寮傚父: address={}", address, e);
+ return null;
+ }
}
/**
@@ -165,4 +216,44 @@
throw new RuntimeException("楂樺痉鍦板浘璺緞瑙勫垝寮傚父", e);
}
}
+
+ public static JSONObject directionInfo(String mode, String from, String to) {
+ // 楂樺痉鍧愭爣绯讳负 lng,lat
+ String[] fromArr = from.split(",");
+ String[] toArr = to.split(",");
+ String origin = fromArr[1] + "," + fromArr[0]; // lng,lat
+ String destination = toArr[1] + "," + toArr[0]; // lng,lat
+
+ try {
+ String url;
+ if ("bicycling".equals(mode)) {
+ url = BICYCLING_URL
+ + "?key=" + amapKey
+ + "&origin=" + URLEncoder.encode(origin, "UTF-8")
+ + "&destination=" + URLEncoder.encode(destination, "UTF-8");
+ } else {
+ // 榛樿椹捐溅
+ url = DRIVING_URL
+ + "?key=" + amapKey
+ + "&origin=" + URLEncoder.encode(origin, "UTF-8")
+ + "&destination=" + URLEncoder.encode(destination, "UTF-8");
+ }
+
+ log.info("楂樺痉鍦板浘璺緞瑙勫垝璇锋眰: mode={}, from={}, to={}", mode, from, to);
+
+ JSONObject json = new Http().build(url)
+ .setConnectTimeout(5000)
+ .setReadTimeout(10000)
+ .get()
+ .toJSONObject();
+
+ log.info("楂樺痉鍦板浘璺緞瑙勫垝鍝嶅簲: {}", json);
+ return json;
+ } catch (IOException e) {
+ log.error("楂樺痉鍦板浘璺緞瑙勫垝寮傚父", e);
+ throw new RuntimeException("楂樺痉鍦板浘璺緞瑙勫垝寮傚父", e);
+ }
+ }
+
+
}
--
Gitblit v1.9.3