Stay organized with collections Save and categorize content based on your preferences.
You can retrieve a set of routes from the Routes Preferred API by sending an HTTP POST request to the following resource:
https://routespreferred.googleapis.com/v1:computeRoutes
Include your request options, formatted in JSON, in the message body. For information about the complete set of options, see the computeRoutes
Request Body.
The following JSON code demonstrates how to construct a typical request body for a computeRoutes
request.
POST /v1:computeRoutes
Host: routespreferred.googleapis.com
Content-Type: application/json
X-Goog-Api-Key: YOUR_API_KEY
X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline
{
"origin":{
"location":{
"latLng":{
"latitude": 37.419734,
"longitude": -122.0827784
}
}
},
"destination":{
"location":{
"latLng":{
"latitude": 37.417670,
"longitude": -122.079595
}
}
},
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE",
"polylineQuality": "OVERVIEW",
"departureTime": "2019-10-15T15:01:23.045123456Z",
"computeAlternativeRoutes": false,
"routeModifiers": {
"avoidTolls": false,
"avoidHighways": false,
"avoidFerries": false
},
"languageCode": "en-US",
"units": "IMPERIAL"
}
Example response body
The following JSON code is an example of the response body returned from the above call to computeRoutes
.
{
"routes": [
{
"distanceMeters": 772,
"duration": "165s",
"polyline": {
"encodedPolyline": "ipkcFfichVnP@j@BLoFVwM{E?"
}
}
]
}
Example gRPC Request
The following shows an example gRPC request.
Gopackage main import ( "context" "fmt" "log" "time" routespreferred "developers.google.com/maps/go/routespreferred/v1" "google.golang.org/api/option" routespb "google.golang.org/genproto/googleapis/maps/routes/v1" "google.golang.org/genproto/googleapis/type/latlng" "google.golang.org/grpc/metadata" ) const ( // https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys credentialsFile = "service-account.json" // Note that setting the field mask to * is OK for testing, but discouraged in // production. // For example, for ComputeRoutes, set the field mask to // "routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline" // in order to get the route distances, durations, and encoded polylines. fieldMask = "*" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() // instantiate a client c, err := routespreferred.NewRoutesPreferredClient(ctx, option.WithCredentialsFile(credentialsFile)) defer c.Close() if err != nil { log.Fatal(err) } // create the origin using a latitude and longitude origin := &routespb.Waypoint{ LocationType: &routespb.Waypoint_Location{ Location: &routespb.Location{ LatLng: &latlng.LatLng{ Latitude: 37.417670, Longitude: -122.0827784, }, }, }, } // create the destination using a latitude and longitude destination := &routespb.Waypoint{ LocationType: &routespb.Waypoint_Location{ Location: &routespb.Location{ LatLng: &latlng.LatLng{ Latitude: 37.417670, Longitude: -122.079595, }, }, }, } // create the request with additional options req := &routespb.ComputeRoutesRequest{ Origin: origin, Destination: destination, TravelMode: routespb.RouteTravelMode_DRIVE, RoutingPreference: routespb.RoutingPreference_TRAFFIC_AWARE, ComputeAlternativeRoutes: true, Units: routespb.Units_METRIC, LanguageCode: "en-us", RouteModifiers: &routespb.RouteModifiers{ AvoidTolls: false, AvoidHighways: true, AvoidFerries: true, }, PolylineQuality: routespb.PolylineQuality_OVERVIEW, } // set the field mask ctx = metadata.AppendToOutgoingContext(ctx, "X-Goog-Fieldmask", fieldMask) // execute rpc resp, err := c.ComputeRoutes(ctx, req) if err != nil { // "rpc error: code = InvalidArgument desc = Request contains an invalid // argument" may indicate that your project lacks access to Routes Preferred log.Fatal(err) } fmt.Printf("Duration of route %d", resp.Routes[0].Duration.Seconds) }Calculating toll fees example
The following example uses the computeRoutes
method to return toll information with an estimated price when a toll pass is used.
This feature is enabled with the routes.travelAdvisory.tollInfo
field mask specified in the request. The toll pass is specified in the route_modifiers
field. The toll price returned is based on the pricing used by the specified pass. If more than one pass is specified, the least expensive pricing is returned.
curl -X POST -d '{
"origin":{
"location":{
"lat_lng":{
"latitude":47.7020056,
"longitude":-122.3479236
}
}
},
"destination":{
"location":{
"lat_lng":{
"latitude":47.6192234,
"longitude": -122.1676792
}
}
},
"travel_mode":"DRIVE",
"route_modifiers":{
"vehicle_info":{
"emission_type": "GASOLINE"
},
"toll_passes": [
"US_MA_EZPASSMA",
"US_WA_GOOD_TO_GO"
]
}
}' \
-H 'Content-Type: application/json' \
-H 'X-Goog-Api-Key: <YOUR_API_KEY>' \
-H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.travelAdvisory.tollInfo,routes.legs.travelAdvisory.tollInfo' \
'https://routespreferred.googleapis.com/v1alpha:computeRoutes'
Response: Note: The Routes Preferred API provides an estimatedPrice
that applies to non-commercial vehicles.
units
and nanos
are omitted when their value is zero.{
"routes": [
{
"legs": [
{
"travelAdvisory": {
"tollInfo": {
"estimatedPrice": [
{
"currencyCode": "USD",
"units": "3",
"nanos": 400000000
}
]
}
}
}
],
"distanceMeters": 22496,
"duration": "1400s",
"travelAdvisory": {
"tollInfo": {
"estimatedPrice": [
{
"currencyCode": "USD",
"units": "3",
"nanos": 400000000
}
]
}
}
}
]
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["Retrieve routes using the Routes Preferred API by sending an HTTP POST request to `https://routespreferred.googleapis.com/v1:computeRoutes` with request options in JSON format."],["Customize route calculations by specifying parameters like origin, destination, travel mode, routing preferences, departure time, and route modifiers in the request body."],["Responses include route details such as distance, duration, and encoded polylines, with optional toll information and estimated prices when requested."],["The API supports both REST and gRPC interfaces for requesting routes, providing flexibility in integration."],["Toll price estimates are for non-commercial vehicles and may differ for commercial use."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4