简体   繁体   English

使用纬度-经度计算两个地方之间的距离,在java中得到错误的距离

[英]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);
}

Your formula is correct, the problem come from bad use of google map 您的公式正确,问题出在Google地图使用不当

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.

相关问题 通过从EditText输入纬度和经度来计算两个位置之间的距离 - calculate distance between two location by enter latitude and longitude from EditText 使用纬度经度计算两点之间的距离? - Calculating distance between two points, using latitude longitude? 使用GPS纬度经度获取不正确的距离 - Getting incorrect distance using gps latitude longitude J2ME - 计算2纬度和经度之间的距离 - J2ME - Calculate the distance between 2 latitude and longitude 如何使用纬度,经度和半径(距离)将区域划分为更小尺寸的区域,在Google中使用Java搜索API? - How to divide the region into smaller size regions using latitude, longitude and radius(distance), in Google place Search API in Java? 给定纬度和经度,使用谷歌地图计算距离 - Calculate distance using google map given a latitude and longitude 如何使用经度和纬度计算距离和方位? - How can I calculate distance and bearing using latitude and longitude? 如何在经纬度两点之间推进X距离(米)? - How to advance X distance (meters) between two points of latitude and longitude? 是否使用“纬度—经度”返回用户位置可用的最近地点? - Return Nearest Place available to Users location using Latitude-Longitude? 根据距离计算经纬度的点 - Calculate a point from latitude and longitude with a distance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM