From 53857f42f777e2b9753b8f00cce1a60ce3dcb8fd Mon Sep 17 00:00:00 2001
From: Riku <risaku@163.com>
Date: 星期三, 15 十月 2025 22:42:29 +0800
Subject: [PATCH] 2025.10.15 修改高德地图地理逆编码结果,让地理位置信息更加详细
---
src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 152 insertions(+), 4 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 f4aa432..2740da2 100644
--- a/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
+++ b/src/main/kotlin/com/flightfeather/uav/common/net/AMapService.kt
@@ -1,19 +1,167 @@
package com.flightfeather.uav.common.net
+import com.flightfeather.uav.common.exception.BizException
+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
+
/**
* 楂樺痉鍦板浘Web鏈嶅姟API
+ * Date: 2024/07/14
*/
object AMapService {
+ private const val TAG = "AMapService"
+ private const val KEY = "b36a93bac8950d3d7c6c06f21133de51"
+
private val httpMethod = HttpMethod("restapi.amap.com", 443, true)
- private val KEY = ""
+
+ data class AMapAddress(
+ val country: String,
+ val province: String,
+ val city: String,
+ val citycode: String,
+ val district: String,
+ val adcode: String,
+ val township: String,
+ val towncode: String,
+ val address: String,
+ val streetNumber: String,
+ val roadinter: 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}")
+ }
+ }
/**
* 鍦扮悊閫嗙紪鐮�
+ * @param location 鍧愭爣鐐�
+ * @return 鎵�鍦ㄨ閬�
*/
- fun reGeo(location:List<Pair<Double, Double>>) {
- httpMethod.get("v3/geocode/regeo", listOf(
+ fun reGeo(location: Pair<Double, Double>): AMapAddress {
+ val res = httpMethod.get(
+ "/v3/geocode/regeo", listOf(
+ "key" to KEY,
+ "location" to "${location.first},${location.second}",
+ "extensions" to "all"
+ )
+ )
+ val obj = handleRes(res)
+ try {
+ val regeocode = obj["regeocode"].asJsonObject
+ val a = regeocode["addressComponent"].asJsonObject
+ val streetNumber = a["streetNumber"].asJsonObject
+ val roads = regeocode["roads"].asJsonArray
+ val roadinters = regeocode["roadinters"].asJsonArray
+ val roadinter = if (roadinters.size() > 0) roadinters.get(0).asJsonObject else null
+ return AMapAddress(
+ a["country"].asString,
+ a["province"].asString,
+ "",
+ a["citycode"].asString,
+ a["district"].asString,
+ a["adcode"].asString,
+ a["township"].asString,
+ a["towncode"].asString,
+ regeocode["formatted_address"].asString,
+ streetNumber["street"].asString + streetNumber["number"].asString
+ + streetNumber["direction"].asString + streetNumber["distance"].asDouble.toInt() + "绫�",
+ if(roadinter == null) "" else roadinter.get("first_name")?.asString +"鍜�" + roadinter.get("second_name")?.asString + "浜ゅ弶鍙�"
+ + roadinter.get("direction")?.asString + roadinter.get("distance")?.asDouble?.toInt() + "绫�",
+ )
+ } catch (e: Exception) {
+ throw BizException("楂樺痉API鍧愭爣杞崲閿欒锛�${e.message}", e.cause)
+ }
+ }
- ))
+ /**
+ * 鍧愭爣杞崲
+ * @param locations 鍘熷鍧愭爣
+ * @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 locationsStr,
+ "coordsys" to coordsys
+ )
+ )
+ val obj = handleRes(res)
+ try {
+ return obj["locations"].asString.split(";").map {
+ val l = it.split(",")
+ l[0].toDouble() to l[1].toDouble()
+ }
+ } catch (e: Exception) {
+ throw BizException("楂樺痉API鍧愭爣杞崲閿欒锛�${e.message}")
+ }
+ }
+
+ private fun handleRes(res: HttpMethod.MyResponse): JsonObject {
+ if (res.success) {
+ val str = EntityUtils.toString(res.m.entity)
+ val json = JsonParser.parseString(str)
+ return resCheck(json)
+ } else {
+ throw BizException("楂樺痉API缃戣矾閾炬帴閿欒锛岀姸鎬佺爜锛�${res.m.statusLine.statusCode}")
+ }
+ }
+
+ private fun resCheck(json: JsonElement): JsonObject {
+ if (!json.isJsonObject) throw BizException("楂樺痉API澶辫触锛岃繑鍥炲�间笉鏄竴涓猳bject")
+
+ val jo = json.asJsonObject
+ if (jo["status"].asInt != 1) throw BizException("楂樺痉API澶辫触锛岄敊璇�${jo["info"]}")
+
+ return jo
}
}
\ No newline at end of file
--
Gitblit v1.9.3