Javascript 两个 GEO 位置之间的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3838493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distance Between Two GEO Locations
提问by Lennie
I have two GEO locations. How can I calculate the distance between them?
我有两个 GEO 位置。如何计算它们之间的距离?
采纳答案by Christoph
GPS coordinates are geographical coordinates on the WGS84 spheroid. Under this model, Vincenty's formulae gives results of sub-millimeter accuracy. For most applications, that's either overkill or actually deceptive, as the earth's surface is not modelled by WGS84 to that scale.
GPS 坐标是 WGS84 球体上的地理坐标。在这个模型下,Vincenty 的公式给出了亚毫米精度的结果。对于大多数应用程序来说,这要么是矫枉过正,要么实际上具有欺骗性,因为 WGS84 并未按照该比例对地球表面进行建模。
You can compare the accurracy of various methods of distance computation on this page(broken link; check the source codeinstead). As you can see, the spherical and the differential approximations (the latter uses the Pythagorean theorem with the correct local metric) are inaccurate for a lot of cases.
您可以比较各种距离计算方法的精度 在本页(破链路;检查源代码代替)。如您所见,球面近似和微分近似(后者使用具有正确局部度量的勾股定理)在很多情况下都不准确。
Of the remaining methods, the first one uses the spheroid's mean radius to convert from geographical to geocentrical latitude, whereas the second one uses the cosine rule to get more accurate results in case of 'small' distances (where the definition of 'small' mainly depends on the difference in latitude).
其余方法中,第一种使用椭球体的平均半径将地理纬度转换为地心纬度,而第二种使用余弦规则在“小”距离的情况下获得更准确的结果(其中“小”的定义主要是取决于纬度的差异)。
A seperate script containing only these two methods can be found here, which provides a function called distance()
and expecting four arguments: the two latitudes, the difference in longitude (all in radians) and a boolean flag indicating whether the distance is 'small'.
可以在此处找到仅包含这两种方法的单独脚本,该脚本提供了一个名为distance()
并需要四个参数的函数:两个纬度、经度差(均以弧度表示)和指示距离是否“小”的布尔标志。
回答by powtac
If you use the Google Maps API v3 you can calculate the distance as follows:
如果您使用 Google Maps API v3,您可以按如下方式计算距离:
Include the Google MapsJavaScript file with the geometry library:
在几何库中包含Google MapsJavaScript 文件:
http://maps.google.com/maps/api/js?sensor=true&libraries=geometry
The distance can be measured now by using the computeDistanceBetween()
method:
现在可以使用以下computeDistanceBetween()
方法测量距离:
var from = new google.maps.LatLng(49.004, 8.456);
var to = new google.maps.LatLng(49.321, 8.789);
var dist = google.maps.geometry.spherical.computeDistanceBetween(from, to);
回答by unwind
回答by Spudley
It depends on what level of accuracy you want.
这取决于您想要什么级别的准确度。
You could work it out by basic triangle trigonomoetry - ie work out the difference between their longitude, that's one side; then the diff between their latitude, that's the second. Now you can calculate the third side (ie the actual distance between the two) easily enough with basic junior school maths.
你可以通过基本的三角三角法来计算——即计算出它们的经度差,这是一侧;然后是他们纬度之间的差异,这是第二个。现在,您可以使用基本的初中数学轻松计算第三边(即两者之间的实际距离)。
However, that method ignores the curvature of the earth's surface, so if you need to take that into account, you'll need to start getting a bit more clever. But you won't need to worry about that unless the distances are quite large or you need an very high degree of accuracy. For most purposes the basic trig method is fine.
然而,这种方法忽略了地球表面的曲率,所以如果你需要考虑到这一点,你需要开始变得更聪明一些。但是除非距离非常大或者您需要非常高的精度,否则您无需担心这一点。对于大多数用途,基本的触发方法很好。
The other point, of course is that these methods give you a straight-line measurement. This may be what you want, but you may also want to know the distance to travel - ie on the road. This is completely different, as you'd need to have an accurate map of all the relevant roads. If this is what you need, it might be easier to delegate to Google's maps service (or one of several other similar alternatives).
另一点当然是这些方法给你一个直线测量。这可能是您想要的,但您可能还想知道旅行的距离 - 即在路上。这是完全不同的,因为您需要拥有所有相关道路的准确地图。如果这是您所需要的,那么委托给 Google 的地图服务(或其他几个类似的替代方案之一)可能会更容易。