Choose fields to return
When you request a route or route matrix, you must use a field mask to specify what information the response should return. There is no default list of returned fields. If you don't specify a field mask, the methods return an error.
Using a field mask also ensures that you don't request unnecessary data, which in turn helps with response latency and avoids unnecessary charges for information your system doesn't need.
You specify the list of the fields you need in a response field mask. You then pass
the response field mask to either method by using the URL parameter $fields
or
fields
, or by using the HTTP or gRPC header X-Goog-FieldMask
.
For more information about URL parameters, see System Parameters.
Define a response field mask
The response field mask is a comma-separated list of paths, where each path specifies a unique field in the response message. The path starts from the top-level response message and uses a dot-separated path to the specified field.
Construct and specify a field path as follows:
- Find the fields that contain the information you need from the Routes API. For details, see Field References.
- Determine the paths for the fields you need and construct the field masks for them: For details, see Determine what field mask you want to use.
Combine the field masks for all of the fields you need, separating the field masks with commas. For example, to request the
distanceMeters
for the route leg, plus the duration for each route leg step, enter them both, separated by a comma, with no spaces:routes.legs.distanceMeters,routes.legs.steps.duration
Send the field mask with your API request. For example, in a cURL request, you would specify the field mask with
-H
andX-Goog-FieldMask
:-H X-Goog-FieldMask: routes.legs.distanceMeters,routes.legs.steps.duration
Field References
To see the fields that you can request in a response through field masks, refer to the Routes API references linked in the following list. Specify fields in camel case as shown in the reference. For example, routePreference
.
These references include the fields that are available; however, you need to refer to the hierarchy of the fields to determine the full field mask path. For details on getting the hierarchy of the fields, see Determine what field mask you want to use.
- Compute route field mask
- Compute route matrix field masks
- REST: Specifies the fields of the Response body to return.
- gRPC: Specifies the fields of the RouteMatrixElement object in the response to return.
Determine what field masks to use
Here's how you can determine which fields you want to use, and construct the field masks for them:
- Request all fields using a field mask of
*
. - Look at the hierarchy of the fields in the response for the fields you want.
Construct your field masks using the hierarchy of the fields shown in the previous step, using this format:
topLevelField[.secondLevelField][.thirdLevelField][...]
For example, for this partial response from a route:
"routes": [ { "legs": [ { "steps": [ {"distanceMeters": 119}, {"distanceMeters": 41} ] } ], "distanceMeters": 160 } ]
If you want to return only the distanceMeters
field for the route leg; that is, the last distanceMeters
in the preceding sample, your field mask is as follows:
routes.legs.distanceMeters
If you want instead want to return the distanceMeters
field for each step of the route leg; that is, the distanceMeters
under steps
in the preceding sample, your field mask is as follows:
routes.legs.steps.distanceMeters
If you want to return both, with the result above, your field mask is as follows:
routes.legs.distanceMeters,routes.legs.steps.distanceMeters
Example field mask paths
This section contains more examples on how to specify a field path as part of a response field mask in REST and gRPC calls.
REST call to computeRoutes
In the first example, you use a REST call to the
computeRoutes
method to calculate
a route. In this example, in the header, you specify field masks to return the route distanceMeters
and duration
fields in the response. Remember to prefix the field name by routes
.
X-Goog-FieldMask: routes.distanceMeters,routes.duration
REST call to computeRouteMatrix
For the REST computeRouteMatrix
method used to compute a route matrix, in the header, specify to return
originIndex
, destinationIndex
, and duration
for each combination of origin
and destination:
X-Goog-FieldMask: originIndex,destinationIndex,duration
gRPC call
For gRPC, set a variable containing the response field mask. You can then pass that variable to the request.
const ( fieldMask = "routes.distanceMeters,routes.duration,routes.polyline.encodedPolyline" )
Field path considerations
Include only the fields that you require in the response to return just the fields that you need:
- Decreases processing times, so your results are returned with a lower latency.
- Ensures stable latency performance. If you select all fields, or if you select all fields at the top level, you might experience performance degradation when new fields are added and then are automatically included in your response.
- Results in a smaller response size, which translates into higher network throughput.
- Ensures that you don't request unnecessary data, which helps to avoid unnecessary processing time and billed charges.
For more details on constructing a field mask, see the field_mask.proto.