| | |
| | | |
| | | /** |
| | | * 高德地图Web服务API |
| | | * Date: 2024/07/14 |
| | | */ |
| | | object AMapService { |
| | | |
| | |
| | | val towncode: String, |
| | | val street: String, |
| | | ) |
| | | |
| | | data class AMapDirection( |
| | | // 路线类型,driving: 驾车; |
| | | 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}") |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 地理逆编码 |
| | |
| | | a["streetNumber"].asJsonObject["street"].asString, |
| | | ) |
| | | } catch (e: Exception) { |
| | | throw BizException("高德API坐标转换错误,${e.message}") |
| | | throw BizException("高德API坐标转换错误,${e.message}", e.cause) |
| | | } |
| | | } |
| | | |