1

I cannot get alternative routes using DirectionsService.route with provideRouteAlternatives set to true and with several waypoints defined.

But nothing in the doc states that it is impossible. So am I just unlucky or do you also experience the same issue ?

(It does work when I remove the waypoints...)

Because some have request my implementation, I've done a quick jsFiddle that shows the problem. Check the console logs to see the result with and without waypoints.

var directionsService;

function initMap() {
  directionsService = new google.maps.DirectionsService();
  computeFakeItinerary();
}

function computeFakeItinerary() {
  var start = {
    lat: 45.7579555,
    lng: 4.8351209
  };
  var end = {
    lat: 43.7383216,
    lng: 7.4069485
  };
  var waypoints = [{
    location: {
      lat: 45.18236,
      lng: 5.6997148
    },
    stopover: true
  }];

  directionsService.route({
    origin: start,
    provideRouteAlternatives: true,
    waypoints: waypoints,
    destination: end,
    travelMode: 'DRIVING'
  }, routeCallback);

  directionsService.route({
    origin: start,
    provideRouteAlternatives: true,
    waypoints: [],
    destination: end,
    travelMode: 'DRIVING'
  }, routeCallback);
}

function routeCallback(data, status) {
  if (status == 'OK') {
    if (data.geocoded_waypoints.length > 2) {
      console.log("With waypoints there is " + data.routes.length + " routes");
    } else {
      console.log("Without waypoints there is " + data.routes.length + " routes");

    }
  }
}

initMap();
<script src="https://maps.googleapis.com/maps/api/js">
</script>
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44
  • How did you do it? Where is your code? Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that exhibits your issue. – MrUpsidown Feb 01 '17 at 16:42
  • ok I will, but the question was not really code related. – Valentin Coudert Feb 01 '17 at 16:53
  • How can I test it? How could anyone help? You don't even give a route example that we can test. So yes please do so. – MrUpsidown Feb 01 '17 at 16:56
  • related question: [How to Draw Multiple Possible Routes in Google Map from Source to Destination](http://stackoverflow.com/questions/38842844/how-to-draw-multiple-possible-routes-in-google-map-from-source-to-destination). I agree that there is nothing in the documentation that states that it isn't possible, making this either a bug in the documentation or in the API (I'm guessing the bug is in the documentation). You could try creating [an issue in the issue tracker](https://code.google.com/p/gmaps-api-issues/) – geocodezip Feb 01 '17 at 17:30
  • @geocodezip I just did, thanks. – Valentin Coudert Feb 01 '17 at 18:59
  • @MrUpsidown I assume you downvoted the issue. You now have the fiddle :) – Valentin Coudert Feb 01 '17 at 18:59
  • Just a jsfiddle is not adequate. You should provide a [mcve] **in the question itself**. – geocodezip Feb 01 '17 at 19:06
  • 2
    [Issue in the issue tracker: Issue 11379: Bug: no route alternatives with waypoints](https://code.google.com/p/gmaps-api-issues/issues/detail?id=11379) – geocodezip Feb 01 '17 at 19:09
  • The same happens on http://maps.google.com. I know it's not a reference for the API functionality but that might well be how it is. Should be mentioned in the docs though. – MrUpsidown Feb 01 '17 at 20:40

1 Answers1

1

Official answer from Google:

Thanks for alerting us to this.

It appears to be a documentation issue. We provide alternatives routes for queries without optimize:true and without waypoints.

We requested to update the documentation.

Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44