All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.feizns.dynamic.math.Distances Maven / Gradle / Ivy

There is a newer version: 6.1-RELEASE
Show newest version
package com.gitee.feizns.dynamic.math;

/**
 * 地图计算两点之间的距离,计算距离的工具类
 * @link 计算两个经纬度之间的距离
 * @author feizns
 * @since 2019/5/9
 */
public class Distances {

    /**
     * 默认地球半径
     */
    public static final double EARTH_RADIUS = 6378137.0;

    /**
     * 转化为弧度(rad)
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     *
     * double distance = DistanceUtils.getDistance(A.getLatitude(), A.getLongitude(), B.getLatitude(), B.getLongitude());
     * 计算两个经纬度之间的距离 单位(m)
     * @param lat1 1点经度
     * @param lng1 1点纬度
     * @param lat2 2点经度
     * @param lng2 2点纬度
     * @return 距离(单位:米)
     */
    public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        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.0;
        return s;
    }

    /**
     * 计算两个经纬度之间的距离 单位(m)
     * @see Distances#getDistance(double, double, double, double)
     * @param a 经纬度点A
     * @param b 经纬度点B
     * @return 距离(单位:米)
     */
    public static double getDistance(Point a, Point b) {
        return getDistance(a.latitude, a.longitude, b.latitude, b.longitude);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy