Javascript 从Google maps directions V3获取多段线

Javascript 从Google maps directions V3获取多段线,javascript,google-maps-api-3,Javascript,Google Maps Api 3,在得到方向后,我试图在谷歌地图上得到一条多段线。我想使用沿多段线放置标记 var polyline = new google.maps.Polyline({ path: [], strokeColor: '#FF0000', strokeWeight: 3 }); var bounds = new google.maps.LatLngBounds(); var legs = response.routes[0].legs; for (i=0;i<legs.length;i+

在得到方向后,我试图在谷歌地图上得到一条多段线。我想使用沿多段线放置标记

var polyline = new google.maps.Polyline({
  path: [],
  strokeColor: '#FF0000',
  strokeWeight: 3
});
var bounds = new google.maps.LatLngBounds();


var legs = response.routes[0].legs;
for (i=0;i<legs.length;i++) {
  var steps = legs[i].steps;
  for (j=0;j<steps.length;j++) {
    var nextSegment = steps[j].path;
    for (k=0;k<nextSegment.length;k++) {
      polyline.getPath().push(nextSegment[k]);
      bounds.extend(nextSegment[k]);
    }
  }
}

polyline.setMap(map);
map.fitBounds(bounds);
我找到了这篇文章,它很有效,但我发现它并不完全准确。在图像中,Google方向为浅蓝色,多段线为深蓝色:

你可以看到这是非常不准确的

代码如下:

directions_service.route(req, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    path = response.routes[0].overview_path;
    $(path).each(function(index, item) {
      route.getPath().push(item);
      bounds.extend(item);
    });
    route.setMap(map);
    map.fitBounds(bounds);
    directions_display.setDirections(response);
  }
});
有人知道如何提高该精度或以其他方式获得多段线吗

编辑:

我想添加使其工作的代码:

legs = response.routes[0].legs;
$(legs).each(function(index, item) {
  steps = item.steps;
  $(steps).each(function(index, item) {
    path = item.path;
    $(path).each(function(index, item) {
      route.getPath().push(item);
      counter++;
      bounds.extend(item);
    });
  });
});

不要对多段线使用概述路径,它不包括所有点,从所有支腿中抓住点并使用它们创建多段线

var polyline = new google.maps.Polyline({
  path: [],
  strokeColor: '#FF0000',
  strokeWeight: 3
});
var bounds = new google.maps.LatLngBounds();


var legs = response.routes[0].legs;
for (i=0;i<legs.length;i++) {
  var steps = legs[i].steps;
  for (j=0;j<steps.length;j++) {
    var nextSegment = steps[j].path;
    for (k=0;k<nextSegment.length;k++) {
      polyline.getPath().push(nextSegment[k]);
      bounds.extend(nextSegment[k]);
    }
  }
}

polyline.setMap(map);
map.fitBounds(bounds);