By default, the Routes API returns thedefaultroute, which is typically the fastest route from the origin to the destination. When you request alternativeroutes, the API returns up to three routes along with the default route. Your customers can then choose a route that best fits their requirements.
Considerations when requesting alternative routes
To request alternative routes, be aware of the following considerations:
You can only request alternative routes for routes without intermediate waypoints. Requesting alternative routes when the route specifies intermediate waypoints does not cause an error. However, no alternative routes are returned.
The response contains a maximum of three alternative routes. However, sometimes no alternative routes are available so the response only contains the default route.
Because of the additional processing required to calculate alternative routes, requesting alternative routes might increase the response time of the API.
Example alternative routes request
SetcomputeAlternativeRoutes
totrue
to request alternative routes. The
following example shows how to request alternative routes in a
computeRoutes
method (REST) request.
curl -X POST -d '{ "origin":{ "location":{ "latLng":{ "latitude":42.340173523716736, "longitude":-71.05997968330408 } } }, "destination":{ "location":{ "latLng":{ "latitude":42.075698891472804, "longitude": -72.59806562080408 } } }, "travelMode": "DRIVE", "routingPreference": "TRAFFIC_AWARE", "computeAlternativeRoutes": true }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key:YOUR_API_KEY' \ -H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.routeLabels'\ 'https://routes.googleapis /directions/v2:computeRoutes'
In this example, you specify a field mask so that the response contains only the
duration
,distanceMeters,
androuteLabels
properties for each route. Your
customer can then use that information to choose which route to take. For more
information, seeChoose what information to return.
Example alternative routes response
In the response, use therouteLabels
array property to identify reach
route:
For the default route, the
routeLabels
property containsDEFAULT_ROUTE
.For any alternate routes, the
routeLabels
property containsDEFAULT_ROUTE_ALTERNATE
.
In this example, the response contains the default route and two alternative
routes. Notice that the values of theduration
anddistanceMeters
properties
are different for each route:
{ "routes": [ { "distanceMeters": 150322, "duration": "5309s", "routeLabels": [ "DEFAULT_ROUTE" ] }, { "distanceMeters": 157614, "duration": "6879s", "routeLabels": [ "DEFAULT_ROUTE_ALTERNATE" ] }, { "distanceMeters": 189311, "duration": "7376s", "routeLabels": [ "DEFAULT_ROUTE_ALTERNATE" ] } ] }
Include polylines in the response
To make it easier for the user to select the route, add the polylinefor each route to the response. You can then display each polyline on a map.
To add the polyline, includeroutes.polyline
in the field mask:
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.routeLabels,routes.polyline'
The response then contains the polyline for each route:
{ "routes": [ { "distanceMeters": 150322, "duration": "5309s", "polyline": { "encodedPolyline": "mrlaGtavpLPLBTm…PgA^qC" }, "routeLabels": [ "DEFAULT_ROUTE" ] }, { "distanceMeters": 157614, "duration": "6879s", "polyline": { "encodedPolyline": "DmEd`@e@pCo@pCs@z…PgA^qC" }, "routeLabels": [ "DEFAULT_ROUTE_ALTERNATE" ] }, { "distanceMeters": 189311, "duration": "7376s", "polyline": { "encodedPolyline": "FVLL|Af@HPAV…PgA^qC" }, "routeLabels": [ "DEFAULT_ROUTE_ALTERNATE" ] } ] }