Calculate routing summary
To use Text Search (New) or Nearby Search (New) to calculate the travel duration and distance to each place in the response:
-
Pass the
routingParameters.origin
parameter in the request to specify the latitude and longitude coordinates of the routing origin. This parameter is required to calculate the duration and distance to each place in the response. -
Include
routingSummaries
in the field mask so that the response includes theroutingSummaries
array. This array contains the duration and distance from the routing origin to each place in the response.
Use Text Search (New)
In the following request, you calculate the travel duration and distance to each place in the Text Search (New) response:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food in Sydney, Australia", "routingParameters": { "origin": { "latitude": -33.8688, "longitude": 151.1957362 } } }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \ -H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \ 'https://places.googleapis.com/v1/places:searchText'
The response contains two JSON arrays: the places
array contains the matching
places, and the routingSummaries
array containing the duration and distance to
travel to each place:
{ "places": [ { object (Place) } ] "routingSummaries": [ { object (RoutingSummary) } }
Each element in the routingSummaries
array is at the corresponding array
location as the place in the places
array. That is, the element at
routingSummaries[0]
corresponds to the place at places[0]
.
The array length of routingSummaries
is the same as the array length of
places
. In the case where the routingSummary
for a place is not available,
the array entry is empty.
Because this example calculates the duration and distance from the routing
origin to each place, the routingSummaries.legs
field in the response contains a single
Leg
object that contains the duration
and distanceMeters
from
the routing origin to the place.
{ "places": [ { "formattedAddress": "1, Westfield Sydney Central Plaza, 450 George St, Sydney NSW 2000, Australia", "displayName": { "text": "Gözleme King Sydney", "languageCode": "en" } }, { "formattedAddress": "367 Pitt St, Sydney NSW 2000, Australia", "priceLevel": "PRICE_LEVEL_MODERATE", "displayName": { "text": "Mother Chu's Vegetarian Kitchen", "languageCode": "en" } }, … ] "routingSummaries": [ { "legs": [ { "duration": "597s", "distanceMeters": 2607 } ], "directionsUri": "https://www.google.com/maps/dir/-33.8688,151.1957362/''/data=!4m6!4m5!1m0!1m2!1m1!1s0x6b12ae3fa97cd745:0x6aecf365bf497c08!3e0" }, { "legs": [ { "duration": "562s", "distanceMeters": 2345 } ], "directionsUri": "https://www.google.com/maps/dir/-33.8688,151.1957362/''/data=!4m6!4m5!1m0!1m2!1m1!1s0x6b12ae3da97f60c1:0x845f3273bd764f6c!3e0" }, … ] }
From this example, you can see that the duration and distance from the routing origin to the first place in the results is 597 seconds and 2607 meters.
Use Nearby Search
In this example, you calculate the travel duration and distance to each place in the Nearby Search response. This example searches for restaurants in Sydney, Australia and sets the location restriction and the routing origin to the same latitude and longitude coordinate:
curl -X POST -d '{ "includedTypes": ["restaurant"], "maxResultCount": 10, "locationRestriction": { "circle": { "center": { "latitude": -33.8688, "longitude": 151.1957362}, "radius": 500.0 } }, "routingParameters": { "origin": { "latitude": -33.8688, "longitude": 151.1957362 } } }' \ -H 'Content-Type: application/json' -H "X-Goog-Api-Key:API_KEY" \ -H "X-Goog-FieldMask: places.displayName,routingSummaries" \ https://places.googleapis.com/v1/places:searchNearby
You don't have to use the same coordinates for the locationRestriction
and the
for routing origin. For example, you set the locationRestriction
to the center
point of Sydney to bias the search results to that circle. But you then set the
routing origin to the coordinates of your house, meaning to a different location
within the search circle. The request then biases the search results to the
circle, and calculates the routing summaries based on the location of your
house.
Specify travel options
By default, the duration and distance calculations are for a car. However, you can control the vehicle type, as well as other options, in the search.
-
Use the
routingParameters.travelMode
parameter to set the mode of transportation toDRIVE
,BICYCLE
,WALK
, orTWO_WHEELER
. For more information on these options, see Available vehicle types for routes. -
Use the
routingParameters.routingPreference
property to set the routing preference option toTRAFFIC_UNAWARE
(default),TRAFFIC_AWARE
, orTRAFFIC_AWARE_OPTIMAL
. Each option has varying levels of data quality and latency. For more information, see Specify how and if to include traffic data.The
routingParameters.routingPreference
property does affect the directions contained in the Preview (Pre-GA)directionsUri
field because Google Maps displays traffic options when it opens the link. -
Use the
routingParameters.routeModifiers
property to specify toavoidTolls
,avoidHighways
,avoidFerries
, andavoidIndoor
. For more information on these options, see Specify route features to avoid.
In the next example, you specify the travel mode as DRIVE
and to avoid
highways:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food in Sydney, Australia", "routingParameters": { "origin": { "latitude": -33.8688, "longitude": 151.1957362 }, "travelMode":"DRIVE", "routeModifiers": { "avoidHighways": true } } }' \ -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \ -H 'X-Goog-FieldMask: places.displayName,places.formattedAddress,places.priceLevel,routingSummaries' \ 'https://places.googleapis.com/v1/places:searchText'