| | |
| | | package com.flightfeather.uav.common.location |
| | | |
| | | import kotlin.math.PI |
| | | import kotlin.math.cos |
| | | import kotlin.math.sin |
| | | import kotlin.math.* |
| | | |
| | | object CoordinateUtil { |
| | | |
| | | private const val Ea = 6378137 //赤道半径 |
| | | private const val Eb = 6356725 //极半径 |
| | | private const val EARTH_RADIUS = 6371000 // 地球半径,单位为米 |
| | | |
| | | /** |
| | | * 根据坐标点、距离和角度,获取另一个坐标 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算坐标点p2相对于p1的方位角 |
| | | * @return 角度 |
| | | */ |
| | | fun getAngle(lon1: Double, lat1: Double, lon2: Double, lat2: Double): Double { |
| | | val deg2rad = PI / 180 |
| | | val dlat = (lat2 - lat1) * deg2rad |
| | | val dlon = (lon2 - lon1) * deg2rad |
| | | val y = sin(dlon) * cos(lat2 * deg2rad) |
| | | val x = cos(lat1 * deg2rad) * sin(lat2 * deg2rad) - sin(lat1 * deg2rad) * cos(lat2 * deg2rad) * cos(dlon) |
| | | val angel = atan2(y, x) |
| | | return (angel * 180 / PI + 360) % 360 |
| | | } |
| | | |
| | | /** |
| | | * 计算坐标点之间距离 |
| | | */ |
| | | fun calculateDistance(lon1: Double, lat1: Double, lon2: Double, lat2: Double): Double { |
| | | val dLat = Math.toRadians(lat2 - lat1) |
| | | val dLon = Math.toRadians(lon2 - lon1) |
| | | val a = (sin(dLat / 2) * sin(dLat / 2) |
| | | + (cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) |
| | | * sin(dLon / 2) * sin(dLon / 2))) |
| | | val c = 2 * atan2(sqrt(a), sqrt(1 - a)) |
| | | return EARTH_RADIUS * c |
| | | } |
| | | |
| | | /** |
| | | * 纬度相同时 |
| | | * 距离转换为经度 |
| | | */ |