[英]calculate distance between two place using latitude-longitude getting wrong distance in java
i am using haversine formula to calculate distance but i am getting wrong distance actually google map distance is 8.1km but haversine formula is showing 4.06 我正在使用Haversine公式来计算距离,但实际上我输入了错误的距离,谷歌地图距离为8.1公里,但是Haversine公式显示为4.06
private static final int EARTH_RADIUS = 6371; // Approx Earth radius in KM
public static double distance(double startLat, double startLong, double endLat, double endLong) {
double dLat = Math.toRadians((endLat - startLat));
double dLong = Math.toRadians((endLong - startLong));
startLat = Math.toRadians(startLat);
endLat = Math.toRadians(endLat);
double a = haversin(dLat) + Math.cos(startLat) * Math.cos(endLat) * haversin(dLong);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS * c; // <-- d
}
public static double haversin(double val) {
return Math.pow(Math.sin(val / 2), 2);
}
As suggested by Tim in comments, you need to see the point-to-point distance, not road distance 正如Tim在评论中建议的那样,您需要查看点对点距离,而不是道路距离
Using the two distance(17.451955, 78.478187, 17.442504, 78.441323) give 4.06km: 使用两个距离(17.451955、78.478187、17.442504、78.441323)得出4.06公里:
8.1km is probably the distance by road, and not point-to-point 8.1km可能是道路距离,而不是点对点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.