-2

I need to get the shortest route with several waypoints A, B, C, D. So I need the shortest route from A to B, the shortest from B to C and so on. The way I understand is that the API should give me a few alternate routes from A to B, a few alternate routes from B to C... And at the end, I should go through all the leg and choose the shortest one.

But Google Direction API gives me only one route as a response. But if I ask the route from A to B, it returns 3 alternate routes, the same from B to C, etc.

My code:

directionsService.route({
        origin: 'A',
        destination: 'D',
        waypoints: [
           {
              location: 'B',
              stopover: true
           },
        {
              location: 'C',
              stopover: true
           }],
        travelMode: 'DRIVING'
        , provideRouteAlternatives: true
     }, callback);

the response I get is this click to see image

I also tried using stopover: false and got the same result. I cannot use travelMode: 'WALKING' as the distance would be very different.

How can I solve this?

Art Licis
  • 3,619
  • 1
  • 29
  • 49

1 Answers1

0

If you have waypoints, you can't get route alternatives.

One option would be to (if you don't know the way point order), request the route with optimizeWaypoints: true (to get the waypoint order), then request the routes between each set of waypoints with provideRouteAlternatives: true to get the options, then stitch them together. You will have to deal with the UI for selecting the alternative routes.

geocodezip
  • 158,664
  • 13
  • 220
  • 245