返回的waypoint_order为空

时间:2015-07-31 11:18:44

标签: google-maps-api-3

我将所有航点都设置为true,但是将waypoint_order返回为空。

是的,我已将optimizeWaypoints设置为true。这基本上是一个修改过的演示代码。

此处有更多细节,因为我不能发布它。

  var request = {
     origin: start,
     destination: finish,
     waypoints: waypts,
     optimizeWaypoints: true,
     travelMode: google.maps.TravelMode.DRIVING
  };


  directionsService.route(request, function(response, status) {

     if (status == google.maps.DirectionsStatus.OK) {

        directionsDisplay.setDirections(response);
        var route = response.routes[0];
        var summaryPanel = document.getElementById('directions_panel');

        summaryPanel.innerHTML = '';
        // For each route, display summary information.
        for (var i = 0; i < route.legs.length; i++) {
           var routeSegment = i + 1;
           summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
           summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
           summaryPanel.innerHTML += route.legs[i].end_address + '<br>';

           summaryPanel.innerHTML += 'distance text: ' + route.legs[i].distance.text + '<br><br>';
           summaryPanel.innerHTML += 'distance value: ' + route.legs[i].distance.value + '<br><br>';

           summaryPanel.innerHTML += 'duration text: ' + route.legs[i].duration.text + '<br><br>';
           summaryPanel.innerHTML += 'duration value: ' + route.legs[i].duration.value + '<br><br>';

           totalDistance += route.legs[i].distance.value;
           totalDuration += route.legs[i].duration.value;
        }

        summaryPanel.innerHTML += 'total distance: ' + totalDistance + '<br>';
        summaryPanel.innerHTML += 'total duration: ' + totalDuration + '<br>';

        document.getElementById('result').value = summaryPanel.innerHTML;

        summaryPanel.innerHTML += 'waypoint_order: [' + '<br>';

        for (var i=0; i < route.waypoint_order.count; i++) {
           var num = route.waypoint_order[i];
           summaryPanel.innerHTML += num + '<br>';
           // AllMarkers[i].setIcon("http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=" + (num + 1) +"|FF776B|000000")
        }

        summaryPanel.innerHTML += ']' + '<br>';

     }
  });

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。 waypoint_order对象变为空。 在我的情况下,解决方案是在每个航点中设置属性stopover = true。 我希望它有用。

答案 1 :(得分:0)

waypoint_order是一个数组,它没有count属性,应该是length

for (var i=0; i < route.waypoint_order.count; i++) {
   var num = route.waypoint_order[i];
   summaryPanel.innerHTML += num + '<br>';
}

应该是:

for (var i=0; i < route.waypoint_order.length; i++) {
   var num = route.waypoint_order[i];
   summaryPanel.innerHTML += num + '<br>';
}

working fiddle

代码段

var totalDistance = 0;
var totalDuration = 0;

function initialize() {
  var map = new google.maps.Map(document.getElementById('map_canvas'));
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer({
    map: map
  });
  var start = "Bangor, ME";
  var finish = "Tampa, FL";
  var waypts = [{
    location: "Baltimore,MD",
    stopover: true
  }, {
    location: "Newark, NJ",
    stopover: true
  }, {
    location: "New York, NY",
    stopover: true
  }];

  var request = {
    origin: start,
    destination: finish,
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
  };


  directionsService.route(request, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      directionsDisplay.setDirections(response);
      var route = response.routes[0];
      var summaryPanel = document.getElementById('directions_panel');

      summaryPanel.innerHTML = '';
      // For each route, display summary information.
      for (var i = 0; i < route.legs.length; i++) {
        var routeSegment = i + 1;
        summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment + '</b><br>';
        summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
        summaryPanel.innerHTML += route.legs[i].end_address + '<br>';

        summaryPanel.innerHTML += 'distance text: ' + route.legs[i].distance.text + '<br><br>';
        summaryPanel.innerHTML += 'distance value: ' + route.legs[i].distance.value + '<br><br>';

        summaryPanel.innerHTML += 'duration text: ' + route.legs[i].duration.text + '<br><br>';
        summaryPanel.innerHTML += 'duration value: ' + route.legs[i].duration.value + '<br><br>';

        totalDistance += route.legs[i].distance.value;
        totalDuration += route.legs[i].duration.value;
      }

      summaryPanel.innerHTML += 'total distance: ' + totalDistance + '<br>';
      summaryPanel.innerHTML += 'total duration: ' + totalDuration + '<br>';

      document.getElementById('result').value = (totalDistance / 1000).toFixed(2) + " km";

      summaryPanel.innerHTML += 'waypoint_order: [' + ' ';

      for (var j = 0; j < route.waypoint_order.length; j++) {
        var num = route.waypoint_order[j];
        summaryPanel.innerHTML += num + ' ';
        // AllMarkers[i].setIcon("http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=" + (num + 1) +"|FF776B|000000")
      }

      summaryPanel.innerHTML += ']' + '<br>';

    }
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
body,
html,
#map_canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
<div id="directions_panel"></div>
<input id="result" />