如何使用JavaScript在Google地图上获得两个形状之间的最短距离?

时间:2013-03-01 08:14:36

标签: javascript google-maps google-maps-api-3 maps distance

我在我的应用程序中使用Google Maps API(带有MVC的ASP.NET)。

我有一个坐标数组(每个坐标由纬度和经度组成),我们称它为“原点”(这可以是多边形,折线或标记)和另一个坐标数组,让我们称之为“目的地”(也可以是多边形,折线或标记。)

我想计算“原点”和“目的地”之间的最短距离。我怎么能这样做?

4 个答案:

答案 0 :(得分:1)

一种解决方案是找到here找到的一个选项,并计算从原点中每个点到目的地中每个点的距离。最小的距离是两个形状之间的距离。

代码可能看起来像这样(未经测试):

var minDistance = Number.POSITIVE_INFINITY;
for (var i=0; i<origin.length; i++){
   for (var j=0; j<destination.length; j++){
      var dist = google.maps.geometry.spherical.computeDistanceBetween(origin[i], destination[j]);
      if (dist < minDistance) 
         minDistance = dist;
   }
}

如果性能问题,这可能会得到优化。有关这方面的更多信息,我会看一下question及其解决同一问题的答案,尽管从纯粹的数学角度来看。

答案 1 :(得分:1)

我建议您使用余弦球面定律来计算点之间的距离。如果您有一个原点的纬度和经度数组,以及目的地的纬度和经度坐标数组,那么您可以这样做:

var origins = [{lat: "35.5", lon: "-80.0"}, ...]; // Array of origin coordinates
var destinations = [{lat: "34.5", lon: "-80.0"}, ...]; // Array of destination coordinates
var shortestDistance = null;
var shortestPair = [];

for (i = 0; i < origins.length; i++) {
    for (j = 0; j < destinations.length; j++) {

        var lat1 = origins[i].lat.toRadians();
        var lat2 = destinations[j].lat.toRadians();
        var lon = (destinations[j].lon - origins[i].lon).toRadians();
        var R = 6371; // gives distance in kilometers
        var calcDistance = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon)) * R;

        if (shortestDistance === null || calcDistance < shortestDistance) {
            shortestPair[0] = origins[i]; // Store the origin coordinates
            shortestPair[1] = destinations[j]; // Store the destination coordinates
            shortestDistance = calcDistance; // Update the shortest distance
        }
    }
}

/* After this runs, you'll have the array indexes for the origin and 
destination with the shortest distance as well as the actual distance (kilometers) 
in the array shortestPair and the variable shortestDistance respectively.

For miles, divide shortestDistance by 1.609344
For nautical miles, divide shortestDistance by 1.852

*/

这似乎比尝试使用Maps API进行距离计算更简单。上述公式来自http://www.movable-type.co.uk/scripts/latlong.html。如果您需要更准确的计算,您也可以使用hasrsine公式;它在我链接的页面上详细说明。

答案 2 :(得分:1)

好吧,从数学的角度来看:

你的问题是找到空间点与vektor线或平面之间的最短距离。

因此,如果你在[a1,a2,a3][b1,b2,b3]这样的数组中有你的坐标,那么三维空间中这两点之间的距离就像具有三个元素的毕达哥拉斯定理: sqrt[(a1-b1)²+(a2-b2)²+(a3-b3)²]=d(a,b)

我知道这并不考虑地球的曲率,但对于“短距离”这并不重要。

如果您了解一些数学知识,维基百科文章也可能对您有所帮助。 http://en.wikipedia.org/wiki/Euclidean_distance#Three_dimensions

编辑12.08.14:
要考虑地球的曲率,可以进行简单的计算:
(1)你已经知道了地球的距离
(2)你知道约。地球半径

在已知起点(A)和目的地(B)的情况下,现在构建一个带有地心(C)的三角形。你这样做现在计算(C)(sin / cos / tan)的角度。通过该角度,您现在可以获得地球的长度(包括曲率)。

([地球的边界] / 360°)* [在(C)处的角度] =在地球曲率上从(A)到(B)的实例。

答案 3 :(得分:0)

function moveAlongPath(points, distance, index) {  
  index = index || 0;  

  if (index < points.length && typeof points[index +1] !="undefined") {
    var polyline = new google.maps.Polyline({
      path: [points[index], points[index + 1]],
      geodesic: true,
      strokeColor: '#FF0000',
      strokeOpacity: 1.0,
      strokeWeight: 2
    });

    var distanceToNextPoint = polyline.Distance();
    if (distance <= distanceToNextPoint) {
      return polyline_des(points[index],points[index + 1], distance);
    }
    else {
      return moveAlongPath(points,
            distance - distanceToNextPoint,
            index + 1);
    }
  }
  else {
    return null;
  }  
}