From 8b005a808d6ab8fae1480ed57bdfd68af2dafcd4 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期六, 01 十一月 2025 23:07:49 +0800
Subject: [PATCH] feat:输入出入地址时,会自动计算距离
---
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java | 196 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 196 insertions(+), 0 deletions(-)
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
index 80726d4..ac0efe6 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
@@ -10,6 +10,7 @@
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.common.config.TencentMapConfig;
+import com.ruoyi.common.config.BaiduMapConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@@ -59,6 +60,9 @@
@Autowired
private TencentMapConfig tencentMapConfig;
+
+ @Autowired
+ private BaiduMapConfig baiduMapConfig;
/**
* 鏌ヨ杞﹁締GPS鍧愭爣鍒楄〃
@@ -470,4 +474,196 @@
return AjaxResult.error("璺濈璁$畻澶辫触锛�" + e.getMessage());
}
}
+
+ /**
+ * 鐧惧害鍦板浘鍦扮悊缂栫爜鎺ュ彛浠g悊锛堝湴鍧�杞潗鏍囷級
+ */
+ @Anonymous()
+ @GetMapping("/baidu/geocoding")
+ public AjaxResult baiduGeocoding(String address, String city) {
+ try {
+ // 妫�鏌ュ弬鏁�
+ if (address == null || address.trim().isEmpty()) {
+ return AjaxResult.error("鍙傛暟涓嶅畬鏁达紝缂哄皯鍦板潃淇℃伅");
+ }
+
+ // 鏋勫缓鐧惧害鍦板浘鍦扮悊缂栫爜API URL
+ String url = "https://api.map.baidu.com/geocoding/v3/";
+ String params = "address=" + URLEncoder.encode(address, StandardCharsets.UTF_8.toString()) +
+ (city != null && !city.trim().isEmpty() ?
+ "&city=" + URLEncoder.encode(city, StandardCharsets.UTF_8.toString()) : "") +
+ "&output=json" +
+ "&ak=" + baiduMapConfig.getAk();
+
+ logger.info("鐧惧害鍦板浘鍦扮悊缂栫爜璇锋眰: address={}, city={}", address, city);
+
+ // 鍙戦�丠TTP璇锋眰
+ String response = HttpUtils.sendGet(url, params);
+
+ // 杩斿洖缁撴灉
+ return AjaxResult.success("鏌ヨ鎴愬姛", response);
+ } catch (Exception e) {
+ logger.error("鐧惧害鍦板浘鍦扮悊缂栫爜澶辫触", e);
+ return AjaxResult.error("鍦扮悊缂栫爜澶辫触锛�" + e.getMessage());
+ }
+ }
+
+ /**
+ * 鐧惧害鍦板浘璺嚎瑙勫垝鎺ュ彛浠g悊锛堣绠椾袱涓潗鏍囦箣闂寸殑椹捐溅璺濈锛�
+ */
+ @Anonymous()
+ @GetMapping("/baidu/route/driving")
+ public AjaxResult baiduRouteDriving(String origin, String destination) {
+ try {
+ // 妫�鏌ュ弬鏁�
+ if (origin == null || origin.trim().isEmpty() ||
+ destination == null || destination.trim().isEmpty()) {
+ return AjaxResult.error("鍙傛暟涓嶅畬鏁达紝缂哄皯璧风偣鎴栫粓鐐瑰潗鏍�");
+ }
+
+ // 楠岃瘉鍧愭爣鏍煎紡锛堢含搴�,缁忓害锛�
+ String[] originParts = origin.split(",");
+ String[] destParts = destination.split(",");
+ if (originParts.length != 2 || destParts.length != 2) {
+ return AjaxResult.error("鍧愭爣鏍煎紡閿欒锛屽簲涓猴細绾害,缁忓害");
+ }
+
+ // 鏋勫缓鐧惧害鍦板浘璺嚎瑙勫垝API URL
+ String url = "https://api.map.baidu.com/directionlite/v1/driving";
+ String params = "origin=" + origin +
+ "&destination=" + destination +
+ "&ak=" + baiduMapConfig.getAk();
+
+ logger.info("鐧惧害鍦板浘璺嚎瑙勫垝璇锋眰: origin={}, destination={}", origin, destination);
+
+ // 鍙戦�丠TTP璇锋眰
+ String response = HttpUtils.sendGet(url, params);
+
+ // 杩斿洖缁撴灉
+ return AjaxResult.success("璁$畻鎴愬姛", response);
+ } catch (Exception e) {
+ logger.error("鐧惧害鍦板浘璺嚎瑙勫垝澶辫触", e);
+ return AjaxResult.error("璺嚎瑙勫垝澶辫触锛�" + e.getMessage());
+ }
+ }
+
+ /**
+ * 鐧惧害鍦板浘璁$畻涓や釜鍦板潃涔嬮棿鐨勮窛绂伙紙缁勫悎鎺ュ彛锛氬湴鍧�杞潗鏍� + 璺嚎瑙勫垝锛�
+ */
+ @Anonymous()
+ @GetMapping("/baidu/distance/byAddress")
+ public AjaxResult baiduDistanceByAddress(String fromAddress, String fromCity,
+ String toAddress, String toCity) {
+ try {
+ // 妫�鏌ュ弬鏁�
+ if (fromAddress == null || fromAddress.trim().isEmpty() ||
+ toAddress == null || toAddress.trim().isEmpty()) {
+ return AjaxResult.error("鍙傛暟涓嶅畬鏁达紝缂哄皯璧风偣鎴栫粓鐐瑰湴鍧�");
+ }
+
+ logger.info("寮�濮嬭绠楀湴鍧�璺濈: fromAddress={}, fromCity={}, toAddress={}, toCity={}",
+ fromAddress, fromCity, toAddress, toCity);
+
+ // 绗竴姝ワ細璧风偣鍦板潃杞潗鏍�
+ String geocodingUrl1 = "https://api.map.baidu.com/geocoding/v3/";
+ String geocodingParams1 = "address=" + URLEncoder.encode(fromAddress, StandardCharsets.UTF_8.toString()) +
+ (fromCity != null && !fromCity.trim().isEmpty() ?
+ "&city=" + URLEncoder.encode(fromCity, StandardCharsets.UTF_8.toString()) : "") +
+ "&output=json" +
+ "&ak=" + baiduMapConfig.getAk();
+
+ String geocodingResponse1 = HttpUtils.sendGet(geocodingUrl1, geocodingParams1);
+ logger.info("璧风偣鍦扮悊缂栫爜鍝嶅簲: {}", geocodingResponse1);
+
+ // 瑙f瀽璧风偣鍧愭爣
+ com.alibaba.fastjson2.JSONObject geocodingJson1 = com.alibaba.fastjson2.JSONObject.parseObject(geocodingResponse1);
+ if (geocodingJson1.getInteger("status") != 0) {
+ logger.error("璧风偣鍦扮悊缂栫爜澶辫触: {}", geocodingResponse1);
+ return AjaxResult.error("璧风偣鍦板潃瑙f瀽澶辫触");
+ }
+ com.alibaba.fastjson2.JSONObject location1 = geocodingJson1.getJSONObject("result").getJSONObject("location");
+ double fromLat = location1.getDouble("lat");
+ double fromLng = location1.getDouble("lng");
+ logger.info("璧风偣鍧愭爣: lat={}, lng={}", fromLat, fromLng);
+
+ // 绗簩姝ワ細缁堢偣鍦板潃杞潗鏍�
+ String geocodingUrl2 = "https://api.map.baidu.com/geocoding/v3/";
+ String geocodingParams2 = "address=" + URLEncoder.encode(toAddress, StandardCharsets.UTF_8.toString()) +
+ (toCity != null && !toCity.trim().isEmpty() ?
+ "&city=" + URLEncoder.encode(toCity, StandardCharsets.UTF_8.toString()) : "") +
+ "&output=json" +
+ "&ak=" + baiduMapConfig.getAk();
+
+ String geocodingResponse2 = HttpUtils.sendGet(geocodingUrl2, geocodingParams2);
+ logger.info("缁堢偣鍦扮悊缂栫爜鍝嶅簲: {}", geocodingResponse2);
+
+ // 瑙f瀽缁堢偣鍧愭爣
+ com.alibaba.fastjson2.JSONObject geocodingJson2 = com.alibaba.fastjson2.JSONObject.parseObject(geocodingResponse2);
+ if (geocodingJson2.getInteger("status") != 0) {
+ logger.error("缁堢偣鍦扮悊缂栫爜澶辫触: {}", geocodingResponse2);
+ return AjaxResult.error("缁堢偣鍦板潃瑙f瀽澶辫触");
+ }
+ com.alibaba.fastjson2.JSONObject location2 = geocodingJson2.getJSONObject("result").getJSONObject("location");
+ double toLat = location2.getDouble("lat");
+ double toLng = location2.getDouble("lng");
+ logger.info("缁堢偣鍧愭爣: lat={}, lng={}", toLat, toLng);
+
+ // 绗笁姝ワ細璋冪敤璺嚎瑙勫垝鎺ュ彛璁$畻璺濈
+ String routeUrl = "https://api.map.baidu.com/directionlite/v1/driving";
+ String origin = fromLat + "," + fromLng;
+ String destination = toLat + "," + toLng;
+ String routeParams = "origin=" + origin +
+ "&destination=" + destination +
+ "&ak=" + baiduMapConfig.getAk();
+
+ logger.info("璺嚎瑙勫垝璇锋眰: origin={}, destination={}", origin, destination);
+ String routeResponse = HttpUtils.sendGet(routeUrl, routeParams);
+ logger.info("璺嚎瑙勫垝鍝嶅簲: {}", routeResponse);
+
+ // 瑙f瀽璺濈缁撴灉
+ com.alibaba.fastjson2.JSONObject routeJson = com.alibaba.fastjson2.JSONObject.parseObject(routeResponse);
+ if (routeJson.getInteger("status") != 0) {
+ logger.error("璺嚎瑙勫垝澶辫触: {}", routeResponse);
+ return AjaxResult.error("璺嚎瑙勫垝澶辫触");
+ }
+
+ // 鎻愬彇璺濈淇℃伅锛堝崟浣嶏細绫筹級
+ com.alibaba.fastjson2.JSONObject result = routeJson.getJSONObject("result");
+ com.alibaba.fastjson2.JSONArray routes = result.getJSONArray("routes");
+ if (routes == null || routes.isEmpty()) {
+ logger.error("鏈壘鍒拌矾绾夸俊鎭�");
+ return AjaxResult.error("鏈壘鍒拌矾绾夸俊鎭�");
+ }
+
+ com.alibaba.fastjson2.JSONObject route = routes.getJSONObject(0);
+ int distance = route.getInteger("distance"); // 璺濈锛屽崟浣嶏細绫�
+ int duration = route.getInteger("duration"); // 鏃堕暱锛屽崟浣嶏細绉�
+
+ logger.info("璁$畻鎴愬姛: 璺濈={}绫�, 鏃堕暱={}绉�", distance, duration);
+
+ // 鏋勫缓杩斿洖缁撴灉
+ Map<String, Object> resultMap = new HashMap<>();
+ resultMap.put("distance", distance); // 璺濈锛堢背锛�
+ resultMap.put("duration", duration); // 鏃堕暱锛堢锛�
+ resultMap.put("distanceKm", String.format("%.1f", distance / 1000.0)); // 璺濈锛堝叕閲岋級
+ resultMap.put("durationMin", duration / 60); // 鏃堕暱锛堝垎閽燂級
+
+ // 璧风偣鍧愭爣
+ Map<String, Object> fromLocation = new HashMap<>();
+ fromLocation.put("lat", fromLat);
+ fromLocation.put("lng", fromLng);
+ resultMap.put("fromLocation", fromLocation);
+
+ // 缁堢偣鍧愭爣
+ Map<String, Object> toLocation = new HashMap<>();
+ toLocation.put("lat", toLat);
+ toLocation.put("lng", toLng);
+ resultMap.put("toLocation", toLocation);
+
+ return AjaxResult.success("璁$畻鎴愬姛", resultMap);
+ } catch (Exception e) {
+ logger.error("璁$畻鍦板潃璺濈澶辫触", e);
+ return AjaxResult.error("璁$畻璺濈澶辫触锛�" + e.getMessage());
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.1