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>