xuhy
4 天以前 fe1bb1affe25ba41a0f01c3c07347603a69f31cd
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
package com.ruoyi.system.utils;
 
/**
 * 高德地图的地理编码工具类
 */
public class GDMapGeocodingUtil {
 
    /**
     * 地球半径,单位m
     */
    private static final double EARTH_RADIUS = 6371000;
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
    /**
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离,单位m
     * */
    public static double getDistance(double lon1,double lat1,double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }
 
    public static void main(String[] args) {
        System.err.println(getDistance(104.165799,30.586865,104.155819,31.585843));
    }
 
}