地球经度和纬度的距离在地球表面上并不是简单的直线距离,而是大圆弧的长度。因此,计算两个全球位置之间的距离需要使用数学公式。在Java中,有几种方法可以准确地计算两点之间的距离,几乎可以支持全球任何位置。
一、Haversine公式
Haversine公式是一种常用的计算球面距离的方法。这个公式用来计算两个地球表面上的点之间的距离。
public static double haversine(double startLat, double startLong, double endLat, double endLong) {
final int earthRadius = 6371;
double dLat = Math.toRadians(endLat - startLat);
double dLong = Math.toRadians(endLong - startLong);
startLat = Math.toRadians(startLat);
endLat = Math.toRadians(endLat);
double a = Math.pow(Math.sin(dLat / 2), 2) +
Math.pow(Math.sin(dLong / 2), 2) *
Math.cos(startLat) *
Math.cos(endLat);
double c = 2 * Math.asin(Math.sqrt(a));
return earthRadius * c;
}
这段代码用来计算两个经纬度之间的距离,它将地球看做是一个球形,所以需要知道地球的半径。在这个例子中,半径设置为6371千米。函数参数startLat和startLong是第一个点的纬度和经度,endLat和endLong是第二个点的纬度和经度。这个函数返回两个点之间的距离,以千米为单位。
二、Vincenty公式
Vincenty公式是另一种球面距离计算方法。这个算法比Haversine算法更准确,但也需要更多的计算。这个公式考虑了地球的椭球形状以及海拔高度对距离的影响。
public static double vincenty(double startLat, double startLong, double endLat, double endLong) {
final double earthRadius = 6371; // km
double lat1 = Math.toRadians(startLat);
double lat2 = Math.toRadians(endLat);
double dLong = Math.toRadians(endLong - startLong);
double y = Math.sin(dLong) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLong);
double bearing = Math.atan2(y, x);
double phi1 = Math.atan2(Math.tan(lat1), Math.cos(bearing));
double phi2 = Math.atan2(Math.tan(lat2), Math.cos(bearing));
double a = Math.pow(Math.cos(phi2) * Math.sin(dLong), 2) +
Math.pow(Math.cos(phi1) * Math.sin(phi2) -
Math.sin(phi1) * Math.cos(phi2) * Math.cos(dLong), 2);
double b = Math.sin(phi1) * Math.sin(phi2) +
Math.cos(phi1) * Math.cos(phi2) * Math.cos(dLong);
double c = Math.atan2(Math.sqrt(a), b);
return earthRadius * c;
}
这段代码中的变量和参数与上面的函数类似,使用的是地球半径6371千米。这里的函数使用了发射角(bearing)的概念,它是第一点和第二点之间的连线与正北方向之间的角度。这个函数返回两点之间的距离,以千米为单位。
三、不考虑海拔高度的计算
如果您不需要考虑海拔高度的影响,那么可以使用简单的勾股定理来计算两点之间的距离。下面是一个示例函数:
public static double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return (dist);
}
这个函数返回两点之间的距离,单位是千米。它使用的是经典的勾股定理,但是将地球的曲球面考虑在内。
四、代码示例
public class DistanceCalculator {
public static double haversine(double startLat, double startLong, double endLat, double endLong) {
final int earthRadius = 6371;
double dLat = Math.toRadians(endLat - startLat);
double dLong = Math.toRadians(endLong - startLong);
startLat = Math.toRadians(startLat);
endLat = Math.toRadians(endLat);
double a = Math.pow(Math.sin(dLat / 2), 2) +
Math.pow(Math.sin(dLong / 2), 2) *
Math.cos(startLat) *
Math.cos(endLat);
double c = 2 * Math.asin(Math.sqrt(a));
return earthRadius * c;
}
public static double vincenty(double startLat, double startLong, double endLat, double endLong) {
final double earthRadius = 6371; // km
double lat1 = Math.toRadians(startLat);
double lat2 = Math.toRadians(endLat);
double dLong = Math.toRadians(endLong - startLong);
double y = Math.sin(dLong) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLong);
double bearing = Math.atan2(y, x);
double phi1 = Math.atan2(Math.tan(lat1), Math.cos(bearing));
double phi2 = Math.atan2(Math.tan(lat2), Math.cos(bearing));
double a = Math.pow(Math.cos(phi2) * Math.sin(dLong), 2) +
Math.pow(Math.cos(phi1) * Math.sin(phi2) -
Math.sin(phi1) * Math.cos(phi2) * Math.cos(dLong), 2);
double b = Math.sin(phi1) * Math.sin(phi2) +
Math.cos(phi1) * Math.cos(phi2) * Math.cos(dLong);
double c = Math.atan2(Math.sqrt(a), b);
return earthRadius * c;
}
public static double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist);
dist = Math.toDegrees(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return (dist);
}
}