package com.jsh.erp.util;

public class DistanceCalculatorUtils {
    // 地球的平均半径，单位：米
    private static final double EARTH_RADIUS = 6371000;

    /**
     * 将角度转换为弧度
     * @param degrees 角度值
     * @return 弧度值
     */
    private static double degreesToRadians(double degrees) {
        return degrees * Math.PI / 180;
    }

    /**
     * 根据经纬度计算两点之间的距离，单位为米
     * @param currentLat 当前点的纬度
     * @param currentLon 当前点的经度
     * @param targetLat 目标点的纬度
     * @param targetLon 目标点的经度
     * @return 两点之间的距离，单位：米
     */
    public static double calculateDistance(double currentLat, double currentLon, double targetLat, double targetLon) {
        // 将经纬度从度转换为弧度
        double lat1 = degreesToRadians(currentLat);
        double lon1 = degreesToRadians(currentLon);
        double lat2 = degreesToRadians(targetLat);
        double lon2 = degreesToRadians(targetLon);

        // 计算纬度差和经度差
        double dLat = lat2 - lat1;
        double dLon = lon2 - lon1;

        // Haversine 公式
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(lat1) * Math.cos(lat2) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 计算距离
        return EARTH_RADIUS * c;
    }
}
