package org.dromara.util;
import java.lang.Math;

public class DistanceCalculator {
    private static final double EARTH_RADIUS = 6371000; // 地球平均半径（单位：米）

    public static void main(String[] args) {
        double lat1 = 40.7128; // 第一个点的纬度
        double lon1 = -74.0060; // 第一个点的经度

        double lat2 = 51.5074; // 第二个点的纬度
        double lon2 = -0.1278; // 第二个点的经度

        double distanceInMeters = calculateDistance(lat1, lon1, lat2, lon2);
        System.out.println("两个点之间的距离为：" + distanceInMeters + " 米");
    }

    public static double calculateDistance(String location1, String location2) {
        String[] loat1 = location1.split(",");
        String[] loat2 = location2.split(",");
        return calculateDistance(Double.parseDouble(loat1[1]), Double.parseDouble(loat1[0]), Double.parseDouble(loat2[1]), Double.parseDouble(loat2[0]));
    }

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double radLat1 = Math.toRadians(lat1);
        double radLon1 = Math.toRadians(lon1);
        double radLat2 = Math.toRadians(lat2);
        double radLon2 = Math.toRadians(lon2);

        double a = Math.sin((radLat2-radLat1)/2) * Math.sin((radLat2-radLat1)/2) +
            Math.cos(radLat1) * Math.cos(radLat2) *
                Math.sin((radLon2-radLon1)/2) * Math.sin((radLon2-radLon1)/2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double distance = EARTH_RADIUS * c;

        return distance;
    }
}
