22

在获得路线后,我试图在谷歌地图上获得一条折线。我想使用v3_epoly沿折线放置标记。

我找到了这篇文章,它有效,但我发现它并不完全准确。在图像中,谷歌方向是浅蓝色,折线是深蓝色:

在此处输入图像描述

你可以看到它非常不准确。

这是代码:

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

1 回答 1

52

不要对折线使用overview_path,它不包括所有点,从所有腿中抓取点并使用它们来创建折线。

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

例子

代码片段:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(51.276092, 1.028938),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer({
    map: map,
    preserveViewport: true
  });
  directionsService.route({
    origin: new google.maps.LatLng(51.269776, 1.061326),
    destination: new google.maps.LatLng(51.30118, 0.926486),
    waypoints: [{
      stopover: false,
      location: new google.maps.LatLng(51.263439, 1.03489)
    }],
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      // directionsDisplay.setDirections(response);
      var polyline = new google.maps.Polyline({
        path: [],
        strokeColor: '#0000FF',
        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);
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

于 2013-04-23T23:38:09.207 回答