1、计算两个经纬度点之间的距离
2、代码如下
public class DistanceCalculatorUtils {
private static final double EARTH_RADIUS = 6371000;
public static double haversineDistance(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.pow(Math.sin(dLat / 2), 2) +
Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c;
}
public static void main(String[] args) {
double lat1 = 22.524556;
double lon1 = 113.942227;
double lat2 = 22.524556;
double lon2 = 113.942827;
double distance = haversineDistance(lat1, lon1, lat2, lon2);
System.out.println("两点之间的距离是:" + distance + " 米");
}
}