From 4dd78acfe298217ebc5dd247c5b45c6f33deea9b Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期六, 08 十一月 2025 22:43:14 +0800
Subject: [PATCH] feat:医院选择计算
---
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java | 155 +++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 148 insertions(+), 7 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 5ae3359..1a770d2 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
@@ -575,6 +575,17 @@
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()) +
@@ -586,17 +597,147 @@
String geocodingResponse2 = HttpUtils.sendGet(geocodingUrl2, geocodingParams2);
logger.info("缁堢偣鍦扮悊缂栫爜鍝嶅簲: {}", geocodingResponse2);
- // 瑙f瀽鍧愭爣锛堣繖閲岀畝鍖栧鐞嗭紝瀹為檯搴旇瑙f瀽JSON锛�
- // 娉ㄦ剰锛氶渶瑕佷粠鍝嶅簲涓彁鍙栧潗鏍囷紝杩欓噷杩斿洖涓棿缁撴灉渚涘墠绔鐞�
- Map<String, Object> result = new HashMap<>();
- result.put("fromGeocoding", geocodingResponse1);
- result.put("toGeocoding", geocodingResponse2);
- result.put("message", "璇疯В鏋愬潗鏍囧悗璋冪敤 /baidu/route/driving 鎺ュ彛璁$畻璺濈");
+ // 瑙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);
- return AjaxResult.success("鍦扮悊缂栫爜鎴愬姛", result);
+ // 绗笁姝ワ細璋冪敤璺嚎瑙勫垝鎺ュ彛璁$畻璺濈
+ 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());
}
}
+
+ /**
+ * 鐧惧害鍦板浘鍦板潃鎼滅储鎻愮ずAPI锛堣緭鍏ヨ仈鎯筹級
+ * Place Suggestion API - 鐢ㄤ簬鍦板潃杈撳叆鏃剁殑鏅鸿兘鎻愮ず
+ */
+ @Anonymous()
+ @GetMapping("/baidu/place/suggestion")
+ public AjaxResult baiduPlaceSuggestion(String query, String region) {
+ try {
+ // 妫�鏌ュ弬鏁�
+ if (query == null || query.trim().isEmpty()) {
+ return AjaxResult.error("鍙傛暟涓嶅畬鏁达紝缂哄皯鎼滅储鍏抽敭璇�");
+ }
+
+ // 鏋勫缓鐧惧害鍦板浘 Place Suggestion API URL
+ String url = "https://api.map.baidu.com/place/v2/suggestion";
+ String params = "query=" + URLEncoder.encode(query, StandardCharsets.UTF_8.toString()) +
+ (region != null && !region.trim().isEmpty() ?
+ "®ion=" + URLEncoder.encode(region, StandardCharsets.UTF_8.toString()) : "") +
+ "&output=json" +
+ "&ak=" + baiduMapConfig.getAk();
+
+ logger.info("鐧惧害鍦板浘鍦板潃鎼滅储鎻愮ず璇锋眰: query={}, region={}", query, region);
+
+ // 鍙戦�丠TTP璇锋眰
+ String response = HttpUtils.sendGet(url, params);
+ logger.debug("鐧惧害鍦板浘鍦板潃鎼滅储鎻愮ず鍝嶅簲: {}", response);
+
+ // 瑙f瀽鍝嶅簲
+ com.alibaba.fastjson2.JSONObject jsonResponse = com.alibaba.fastjson2.JSONObject.parseObject(response);
+ if (jsonResponse.getInteger("status") != 0) {
+ logger.error("鍦板潃鎼滅储鎻愮ず澶辫触: {}", response);
+ return AjaxResult.error("鍦板潃鎼滅储澶辫触");
+ }
+
+ // 鎻愬彇鎻愮ず鍒楄〃
+ com.alibaba.fastjson2.JSONArray results = jsonResponse.getJSONArray("result");
+ if (results == null || results.isEmpty()) {
+ logger.info("鏈壘鍒板尮閰嶇殑鍦板潃");
+ return AjaxResult.success("鏌ヨ鎴愬姛", new ArrayList<>());
+ }
+
+ // 鏋勫缓杩斿洖缁撴灉
+ List<Map<String, Object>> suggestions = new ArrayList<>();
+ for (int i = 0; i < results.size(); i++) {
+ com.alibaba.fastjson2.JSONObject item = results.getJSONObject(i);
+
+ Map<String, Object> suggestion = new HashMap<>();
+ suggestion.put("name", item.getString("name")); // 鍚嶇О
+ suggestion.put("address", item.getString("address")); // 鍦板潃
+ suggestion.put("province", item.getString("province")); // 鐪�
+ suggestion.put("city", item.getString("city")); // 甯�
+ suggestion.put("district", item.getString("district")); // 鍖�
+ suggestion.put("uid", item.getString("uid")); // 鍦扮偣UID
+
+ // 缁忕含搴︿俊鎭�
+ com.alibaba.fastjson2.JSONObject location = item.getJSONObject("location");
+ if (location != null) {
+ Map<String, Object> locationMap = new HashMap<>();
+ locationMap.put("lat", location.getDouble("lat"));
+ locationMap.put("lng", location.getDouble("lng"));
+ suggestion.put("location", locationMap);
+ }
+
+ suggestions.add(suggestion);
+ }
+
+ logger.info("鍦板潃鎼滅储鎻愮ず鎴愬姛: 鎵惧埌{} 鏉$粨鏋�", suggestions.size());
+ return AjaxResult.success("鏌ヨ鎴愬姛", suggestions);
+ } catch (Exception e) {
+ logger.error("鍦板潃鎼滅储鎻愮ず澶辫触", e);
+ return AjaxResult.error("鍦板潃鎼滅储澶辫触锛�" + e.getMessage());
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.1