| | |
| | | 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) |
| | | |
| | |
| | | * @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) |
| | |
| | | |
| | | 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}") |
| | | } |
| | | } |
| | | |