是否有任何方便的方法来计算折线(由谷歌方向生成的路线)和不在该折线上的标记之间的直接(最短)距离?
我发现的唯一方法是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);
}
提前致谢!