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

How to get shortest distance between two shapes on Google Maps, using JavaScript?

本文关键字:之间 短距离 JavaScript 使用 两个 谷歌地图      更新时间:2023-09-26

我在我的应用程序中使用谷歌地图API。. NET MVC).

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

我想计算"原点"answers"终点"之间的最短距离。我该怎么做呢?

嗯,从数学的角度来看:

你的问题是找出空间中一点到向量线或向量平面之间的最短距离。

如果你的坐标是在像[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)在地球曲率上的距离。

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

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。如果你需要更精确的计算,你也可以使用哈弗斯公式;

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

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

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

如果性能有问题,可以对其进行优化。关于这方面的更多信息,我想看看这个问题和它的答案,它们处理的是同样的问题,尽管是从纯数学的角度来看。

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