1

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

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

我想计算“起点”和“终点”之间的最短距离。我怎样才能做到这一点?

4

4 回答 4

2

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

您的问题是找到空间中的一点与矢量线或平面之间的最短距离。

因此,如果您在数组中有坐标,[a1,a2,a3]并且[b1,b2,b3]这 2 个点在 3 维空间中的距离就像勾股定理一样,具有三个元素: 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) 在地球曲率上的距离。

于 2014-08-11T09:10:20.233 回答
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。如果您需要计算更准确,也可以使用半正弦公式;我链接的页面上有详细说明。

于 2014-08-04T21:11:21.940 回答
1

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

代码可能如下所示(未经测试):

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;
   }
}

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

于 2014-08-04T20:57:22.590 回答
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;
  }  
}
于 2014-08-08T06:52:59.447 回答