使用纬度经度计算两点之间的距离?
这是我的尝试,这只是我的代码片段:
final double RADIUS = 6371.01;
double temp = Math.cos(Math.toRadians(latA))
* Math.cos(Math.toRadians(latB))
* Math.cos(Math.toRadians((latB) - (latA)))
+ Math.sin(Math.toRadians(latA))
* Math.sin(Math.toRadians(latB));
return temp * RADIUS * Math.PI / 180;
我使用这个公式来获取纬度和经度:
x = Deg + (Min + Sec / 60) / 60)
Here's my try, it's just a snippet of my code:
final double RADIUS = 6371.01;
double temp = Math.cos(Math.toRadians(latA))
* Math.cos(Math.toRadians(latB))
* Math.cos(Math.toRadians((latB) - (latA)))
+ Math.sin(Math.toRadians(latA))
* Math.sin(Math.toRadians(latB));
return temp * RADIUS * Math.PI / 180;
I am using this formulae to get the latitude and longitude:
x = Deg + (Min + Sec / 60) / 60)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Dommer 给出的 Java 代码给出了稍微不正确的结果,但如果您正在处理 GPS 轨迹,这些小错误就会累积起来。这是 Java 中的 Haversine 方法的实现,该方法还考虑了两点之间的高度差。
The Java code given by Dommer gives slightly incorrect results but the small errors add up if you are processing say a GPS track. Here is an implementation of the Haversine method in Java which also takes into account height differences between two points.
这是一个 Java 函数,用于计算两个纬度/经度点之间的距离,贴在下面,以防万一它再次消失。
Here's a Java function that calculates the distance between two lat/long points, posted below, just in case it disappears again.
偶然发现这篇 SOF 文章的未来读者。
显然,这个问题是在 2010 年提出的,现在是 2019 年了。
但它很早就出现在互联网搜索中。最初的问题并不折扣使用第三方库(当我写这个答案时)。
和
https://mvnrepository.com/artifact/org.apache。 lucene/lucene-spatial/8.2.0
请在深入研究之前阅读有关“SloppyMath”的文档!
https://lucene.apache.org/core /8_2_0/core/org/apache/lucene/util/SloppyMath.html
Future readers who stumble upon this SOF article.
Obviously, the question was asked in 2010 and its now 2019.
But it comes up early in an internet search. The original question does not discount use of third-party-library (when I wrote this answer).
and
https://mvnrepository.com/artifact/org.apache.lucene/lucene-spatial/8.2.0
Please read documentation about "SloppyMath" before diving in!
https://lucene.apache.org/core/8_2_0/core/org/apache/lucene/util/SloppyMath.html
注意:此解决方案仅适用于短距离。
我尝试使用多默发布的公式进行应用,发现它在长距离上表现良好,但在我的数据中,我使用的是所有非常短的距离,而多默的帖子表现非常差。我需要速度,更复杂的地理计算效果很好,但太慢了。因此,如果您需要速度并且您所做的所有计算都很短(可能小于 100m 左右)。我发现这个小近似非常有效。请注意,它假设世界是平坦的,因此不要在长距离上使用它,它的工作原理是近似给定纬度处的单个纬度和经度的距离,并返回以米为单位的毕达哥拉斯距离。
Note: this solution only works for short distances.
I tried to use dommer's posted formula for an application and found it did well for long distances but in my data I was using all very short distances, and dommer's post did very poorly. I needed speed, and the more complex geo calcs worked well but were too slow. So, in the case that you need speed and all the calculations you're making are short (maybe < 100m or so). I found this little approximation to work great. it assumes the world is flat mind you, so don't use it for long distances, it works by approximating the distance of a single Latitude and Longitude at the given Latitude and returning the Pythagorean distance in meters.
提供了很多很好的答案,但是我发现了一些性能缺陷,所以让我提供一个考虑到性能的版本。每个常数都是预先计算的,并引入 x,y 变量以避免两次计算相同的值。希望有帮助
was a lot of great answers provided however I found some performance shortcomings, so let me offer a version with performance in mind. Every constant is precalculated and x,y variables are introduced to avoid calculating the same value twice. Hope it helps
这是一个包含各种球面计算的 JavaScript 示例的页面。页面上的第一个应该会提供您所需要的内容。
http://www.movable-type.co.uk/scripts/latlong.html
这是 Javascript 代码,
其中“d”将保持距离。
Here is a page with javascript examples for various spherical calculations. The very first one on the page should give you what you need.
http://www.movable-type.co.uk/scripts/latlong.html
Here is the Javascript code
Where 'd' will hold the distance.
@David George 的答案略有升级:
distance 函数是相同的,但我创建了一个小包装函数,它需要 2 个 Location 对象。因此,如果两个位置实际上都有海拔高度,我只使用 distance 函数,因为有时它们没有。它可能会导致奇怪的结果(如果位置不知道其海拔高度 0 将返回)。在这种情况下,我回到经典的 distanceTo 函数。
Slightly upgraded answer from @David George:
distance function is the same, but I've created I small wrapper function, which takes 2 Location objects. Thanks to this, I only use distance function if both of locations actually have altitude, because sometimes they don't. And it can lead to strange results (if location doesn't know its altitude 0 will be returned). In this case, I fall back to classic distanceTo function.
使用 Java - 这将返回以 KM 为单位的距离。
我们可以通过使用下面的转换公式在 google 电子表格中实现相同的效果 给定单元格 A1 (lat1)、B1 (long1)、C1 (lat2) 中的纬度和经度) 和 D1 (long2)
Using Java - this would return distance in KM.
and same we can achieve in google spread sheet by using below converted formula Given latitudes and longitudes in cells A1 (lat1), B1 (long1), C1 (lat2), and D1 (long2)