有没有一种方便的方法来计算Polyline(由Google Directions生成的路线)与不位于该Polyline上的标记之间的直接(最短)距离?
我找到的唯一方法是手动循环遍历Polyline.getPath()
顶点以计算最短距离,但这似乎有点困难:
var path = routes[0].overview_path;
for (var i = 0; i < data.points.length; i++) {
var latLngA = new LatLng(data.points[i].lat, data.points[i].lng);
var shortest_distance = null;
for (var j = 0; j < path.length; j++) {
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngA, path[i]);
if (shortest_distance == null || distance < shortest_distance) {
shortest_distance = distance;
}
}
console.log(data.points[i].point_title, shortest_distance);
}
提前感谢您!