| | |
| | | package com.flightfeather.uav.common.net |
| | | |
| | | import com.flightfeather.uav.common.exception.BizException |
| | | import com.google.gson.Gson |
| | | 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 |
| | |
| | | val adcode: String, |
| | | val township: String, |
| | | val towncode: String, |
| | | val street: String, |
| | | val address: String, |
| | | val streetNumber: String, |
| | | val roadinter: String, |
| | | ) |
| | | |
| | | data class AMapDirection( |
| | |
| | | /** |
| | | * 驾车路线规划 |
| | | */ |
| | | fun directionDriving(origin: Pair<Double, Double>, destination: Pair<Double, Double>):AMapDirection { |
| | | fun directionDriving(origin: Pair<Double, Double>, destination: Pair<Double, Double>): AMapDirection { |
| | | val res = httpMethod.get( |
| | | "/v5/direction/driving", listOf( |
| | | "key" to KEY, |
| | |
| | | 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>>() |
| | | val finalPaths = mutableListOf<Pair<Double, Double>>() |
| | | path["steps"].asJsonArray.forEach { |
| | | finalPaths.addAll( |
| | | it.asJsonObject["polyline"].asString.split(";").map { str-> |
| | | it.asJsonObject["polyline"].asString.split(";").map { str -> |
| | | val strArr = str.split(",") |
| | | strArr[0].toDouble() to strArr[1].toDouble() |
| | | } |
| | |
| | | * @param location 坐标点 |
| | | * @return 所在街道 |
| | | */ |
| | | 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" |
| | | )) |
| | | 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["adcode"].asString, |
| | | a["township"].asString, |
| | | a["towncode"].asString, |
| | | a["streetNumber"].asJsonObject["street"].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>> { |
| | | 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 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 { |
| | |
| | | } |
| | | } |
| | | |
| | | private fun handleRes(res: HttpMethod.MyResponse):JsonObject { |
| | | private fun handleRes(res: HttpMethod.MyResponse): JsonObject { |
| | | if (res.success) { |
| | | val str = EntityUtils.toString(res.m.entity) |
| | | val json = JsonParser.parseString(str) |