From 7d33080998a2c5b38e8a74dbed2b0f40d39bbe47 Mon Sep 17 00:00:00 2001
From: Riku <risaku@163.com>
Date: 星期三, 04 六月 2025 23:27:26 +0800
Subject: [PATCH] 1. 新增动态污染溯源新的判定逻辑(待完成)

---
 src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt |   65 ++++++++++++++++++++++++++++----
 1 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt b/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
index a9c5497..52e1940 100644
--- a/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
+++ b/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
@@ -5,15 +5,18 @@
 import com.google.gson.JsonElement
 import com.google.gson.JsonObject
 import com.google.gson.JsonParser
+import org.apache.http.util.EntityUtils
+import java.net.URLEncoder
 import java.nio.charset.Charset
 
 /**
  * 楂樺痉鍦板浘Web鏈嶅姟API
+ * Date: 2024/07/14
  */
 object AMapService {
 
     private const val TAG = "AMapService"
-    private const val KEY = "520c5e5cf44c7793104e500cbf0ed711"
+    private const val KEY = "b36a93bac8950d3d7c6c06f21133de51"
 
     private val httpMethod = HttpMethod("restapi.amap.com", 443, true)
 
@@ -28,6 +31,54 @@
         val towncode: String,
         val street: String,
     )
+
+    data class AMapDirection(
+        // 璺嚎绫诲瀷锛宒riving: 椹捐溅锛�
+        val type: String,
+        // 璧风偣缁忕含搴�
+        val origin: Pair<Double, Double>,
+        // 缁堢偣缁忕含搴�
+        val destination: Pair<Double, Double>,
+        // 閫斿緞璺嚎缁忕含搴︼紙涓嶅寘鎷捣鐐圭粓鐐癸級
+        val paths: List<Pair<Double, Double>>,
+        // 鏂规璺濈锛屽崟浣嶏細绫�
+        val distance: String
+    )
+
+    /**
+     * 椹捐溅璺嚎瑙勫垝
+     */
+    fun directionDriving(origin: Pair<Double, Double>, destination: Pair<Double, Double>):AMapDirection {
+        val res = httpMethod.get(
+            "/v5/direction/driving", listOf(
+                "key" to KEY,
+                "origin" to "${origin.first},${origin.second}",
+                "destination" to "${destination.first},${destination.second}",
+                "show_fields" to "polyline"
+            )
+        )
+        val obj = handleRes(res)
+        try {
+            val count = obj["count"].asString.toIntOrNull()
+            if (count != null && count > 0) {
+                val path = obj["route"].asJsonObject["paths"].asJsonArray.get(0).asJsonObject
+                val finalPaths = mutableListOf<Pair<Double,Double>>()
+                path["steps"].asJsonArray.forEach {
+                    finalPaths.addAll(
+                        it.asJsonObject["polyline"].asString.split(";").map { str->
+                            val strArr = str.split(",")
+                            strArr[0].toDouble() to strArr[1].toDouble()
+                        }
+                    )
+                }
+                return AMapDirection("driving", origin, destination, finalPaths, path["distance"].asString)
+            } else {
+                throw BizException("楂樺痉API椹捐溅璺嚎瑙勫垝澶辫触锛屾病鏈夋壘鍒板彲琛岀殑璺嚎")
+            }
+        } catch (e: Exception) {
+            throw BizException("楂樺痉API椹捐溅璺嚎瑙勫垝閿欒锛�${e.message}")
+        }
+    }
 
     /**
      * 鍦扮悊閫嗙紪鐮�
@@ -64,9 +115,10 @@
      * @param coordsys 鍘熷潗鏍囩郴锛屽彲閫夊�硷細gps;mapbar;baidu;autonavi(涓嶈繘琛岃浆鎹�)
      */
     fun coordinateConvert(locations: List<Pair<Double, Double>>, coordsys:String="gps"): List<Pair<Double, Double>> {
+        val locationsStr = URLEncoder.encode(locations.joinToString("|") { "${it.first},${it.second}" }, "UTF-8")
         val res = httpMethod.get("/v3/assistant/coordinate/convert", listOf(
             "key" to KEY,
-            "locations" to locations.joinToString("|") { "${it.first},${it.second}" },
+            "locations" to locationsStr,
             "coordsys" to coordsys
         ))
         val obj = handleRes(res)
@@ -82,16 +134,11 @@
 
     private fun handleRes(res: HttpMethod.MyResponse):JsonObject {
         if (res.success) {
-//            val str = if (res.m.responseCharSet == "utf-8") {
-//                res.m.responseBodyAsString
-//            } else {
-//                String(res.m.responseBody, Charset.forName("utf-8"))
-//            }
-            val str = res.m.responseBodyAsString
+            val str = EntityUtils.toString(res.m.entity)
             val json = JsonParser.parseString(str)
             return resCheck(json)
         } else {
-            throw BizException("楂樺痉API缃戣矾閾炬帴閿欒锛岀姸鎬佺爜锛�${res.m.statusCode}")
+            throw BizException("楂樺痉API缃戣矾閾炬帴閿欒锛岀姸鎬佺爜锛�${res.m.statusLine.statusCode}")
         }
     }
 

--
Gitblit v1.9.3