1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.flightfeather.uav.common.location
 
import kotlin.math.*
 
object CoordinateUtil {
 
    private const val Ea = 6378137 //赤道半径
    private const val Eb = 6356725 //极半径
    private const val EARTH_RADIUS = 6371000 // 地球半径,单位为米
 
    /**
     * 根据坐标点、距离和角度,获取另一个坐标
     * @param pos 坐标点
     * @param len 距离(米)
     * @param radian 弧度
     */
    fun getPointByLen(pos: Pair<Double, Double>, len: Double, radian: Double): Pair<Double, Double> {
        val dx = len * sin(radian)
        val dy = len * cos(radian)
        val ec = Eb + (Ea - Eb) * (90.0 - pos.second) / 90.0
        val ed = ec * cos(pos.second * PI / 180)
        val lng = (dx / ed + pos.first * PI / 180.0) * 180.0 / PI
        val lat = (dy / ec + pos.second * PI / 180.0) * 180.0 / PI
        return Pair(lng, lat)
    }
 
    /**
     * 计算坐标点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
    }
 
    /**
     * 纬度相同时
     * 距离转换为经度
     */
    fun disToLng(distance: Double): Double {
        return distance * 0.00001
    }
}