地球經度和緯度的距離在地球表面上並不是簡單的直線距離,而是大圓弧的長度。因此,計算兩個全球位置之間的距離需要使用數學公式。在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);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291218.html