两个位置之间的距离不正确
我已经在 http://www.movable-type.co 上使用了该算法。 uk/scripts/latlong.html 查找两点之间的距离。
我的两点是
long1 = 51.507467;
lat1 = -0.08776;
long2 = 51.508736;
lat2 = -0.08612;
根据 Movable Type Script 答案是 0.1812km
我的应用程序给出的结果 (d
) 为 0.230km
检查半正弦公式: http://www.movable-type.co.uk/scripts/latlong.html
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(long2-long1);
a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
I have used the algorithm on http://www.movable-type.co.uk/scripts/latlong.html to find the distance between two points.
My two points are
long1 = 51.507467;
lat1 = -0.08776;
long2 = 51.508736;
lat2 = -0.08612;
According to Movable Type Script the answer is 0.1812km
My application gives the result (d
) as 0.230km
Check Haversine formula: http://www.movable-type.co.uk/scripts/latlong.html
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(long2-long1);
a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要重新发明自己的距离计算器, Location 中内置了一个距离计算器 类。
查看
Why to reinvent your own distance calculator, there is one built into the Location class.
Check out
您的实施是正确的。给定这些经度和纬度的距离应产生
0.230 km
的距离。然而,坐标的正常输入是(纬度,经度)。将它们倒过来(经度、纬度)会产生0.1812 km
的错误距离。Your implementation is correct. The distance given those longitudes and latitudes should produce a distance of
0.230 km
. The normal input for coordinates, however, is (latitude, longitude). Putting them in backwards (longitude, latitude) produces the incorrect distance of0.1812 km
.盟友你的概念是正确的。这行可能有点改变
double c = 2 * Math.asin(Math.sqrt(a));
Ally your concept was right.May be a little bit chang in this line
double c = 2 * Math.asin(Math.sqrt(a));