Find shortest route with multiple checkpoints using Google Maps API

I want to determine the shortest path that includes several waypoints such as A, B, C, and D. Essentially, I’m looking for the shortest path from A to B, then B to C, and finally C to D. I understand that the API should provide multiple routes from A to B, and similarly for B to C and C to D. However, the Google Directions API only gives me a single route in response. When I independently request the route from A to B, I do get three alternative routes. Here’s my current code:

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

I’ve tried setting stopover: false but still get the same result. I cannot use travelMode: ‘WALKING’ due to significant distance differences. Any suggestions on resolving this?

have you tried using optimizeWaypoints: true? it might help in getting better overall routes. Otherwise, breaking down your API calls segment-wise is a good approach but API usage will increase, so watch out!

From my experience, optimizing the route using the optimizeWaypoints parameter can sometimes yield a better overall path, but it won’t give multiple alternatives with waypoints included. If you really need alternative routes, breaking down the API calls to handle each segment individually is often the most effective solution. I know it increases API usage, but it allows more control over each part of the journey. Also, consider implementing a custom algorithm to evaluate different route combinations from the segments. This way, you can comprehensively compare and select the best routes based on your criteria.