Avnish
Avnish

Reputation: 21

Waypoint Optimization: Routes API vs Route Optimization API

I understand both Google Maps Routes API and the (newly-launched) Route Optimization API will return the best possible route through multiple stops. Route Optimization API seems like it can handle more advanced scenarios and with higher limits on the number of waypoints. But I'd just like to know how to use it to optimize a set of intermediate waypoints in the most efficient order, based on this example:

curl -X POST -H 'content-type: application/json' -d ' {
  "origin": {
    "address": "Adelaide,SA"
  },
  "destination": {
    "address": "Adelaide,SA"
  },
  "intermediates": [
    {"address": "Barossa+Valley,SA"},
    {"address": "Clare,SA"},
    {"address": "Connawarra,SA"},
    {"address": "McLaren+Vale,SA"}
  ],
  "travelMode": "DRIVE",
  "optimizeWaypointOrder": "true"
  }' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: routes.optimizedIntermediateWaypointIndex' \
'https://routes.googleapis.com/directions/v2:computeRoutes'

Routes API would return the optimized order:

"optimizedIntermediateWaypointIndex": [
                3,
                2,
                0,
                1
            ]

What would the equivalent Route Optimization API OptimizeTours request look like (assuming a single trip)?

(I've reviewed how to construct a request message for OptimizeTours but not sure what properties to use.)

Upvotes: 2

Views: 687

Answers (1)

Fabien Viger
Fabien Viger

Reputation: 141

The Route Optimization API provides an example that does just that. AFAIR, it's more complicated than just using an API key and CURL, but the website describes how to try it. As for the request JSON itself, something like this should work (I translated your addresses to latitude/longitude):

{
  "model": {
    "globalStartTime": "2024-01-01T00:00:00",
    "globalEndTime": "2024-01-01T23:59:59",
    "vehicles": [
      {
        "startLocation": {
          "latitude": -34.9285,
          "longitude": 138.6007
        },
        "endLocation": {
          "latitude": -34.9285,
          "longitude": 138.6007
        },
        "costPerHour": 20.0
      }
    ],
    "shipments": [
      {
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": -34.6182,
              "longitude": 138.9001
            },
          }
        ]
      },
      {
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": -33.8359,
              "longitude": 138.6142
            },
          }
        ]
      },
      {
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": -37.2916,
              "longitude": 140.8336
            },
          }
        ]
      },
      {
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": -35.2192,
              "longitude": 138.5448
            },
          }
        ]
      }
    ]
  }
}

Upvotes: 2

Related Questions