Combine routing summaries with search along route
You can combine routing summary calculations with search along a route. In this case, Text Search (New) returns the travel duration and distance to each place in the response, and then from each place to the final destination of the route.
To use Text Search (New) to calculate the routing summary along with search along a route:
-
Use the Routes API to calculate a route that returns a route polyline in the response.
-
Use the
searchAlongRouteParameters.polyline.encodedPolyline
parameter to pass the route polyline to Text Search (New) to bias the search results to the route. The response then contains places that match the search criteria and are also located near the specified route. -
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.
By default, Text Search (New) performs the search along the entire route:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food", "searchAlongRouteParameters": { "polyline": { "encodedPolyline": "ROUTE_POLYLINE" } } }' \ -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 arrays: the places
array containing the matching
places, and the routingSummaries
array containing the duration and distance to
travel to each place:
{ "places": [ { "formattedAddress": "1477 Plymouth St suite c, Mountain View, CA 94043, USA", "priceLevel": "PRICE_LEVEL_INEXPENSIVE", "displayName": { "text": "Zareen's", "languageCode": "en" } }, { "formattedAddress": "2464 W El Camino Real C, Mountain View, CA 94040, USA", "priceLevel": "PRICE_LEVEL_INEXPENSIVE", "displayName": { "text": "Veggie Garden", "languageCode": "en" } }, … ], "routingSummaries": [ { "legs": [ { "duration": "285s", "distanceMeters": 1616 }, { "duration": "2466s", "distanceMeters": 58147 } ] }, { "legs": [ { "duration": "696s", "distanceMeters": 4704 }, { "duration": "2787s", "distanceMeters": 58901 } ] }, … ] }
For each entry in the legs
array, Text Search (New) returns a two-leg trip
time:
-
The first leg contains the travel duration and distance from the origin to the place. In this example, the duration and distance from the origin to the first place in the results is 285 seconds and 1616 meters.
-
The second leg contains the travel duration and distance from the place to the route destination. In this example, the duration and distance is 2466 seconds and 58147 meters.
Specify the routing origin, travel mode, and route modifiers
You can modify the search and routing summary calculation by specifying the routing origin, travel mode, route modifiers, and routing preferences. The travel mode and route modifiers work the same as for calculating routing summaries without specifying a route as shown in Specify travel options.
By default, the first leg of each result contains the distance from the origin defined by the polyline to each place. However, you can override that default by explicitly specifying a routing origin in the request. If specified, the first leg of all responses specifies the distances and duration from the specified routing origin, overriding the origin from the polyline.
In the next example, you specify a routing origin as the coordinates of San Mateo, CA, specify to avoid tolls, and set the number of results to 5:
curl -X POST -d '{ "textQuery" : "Spicy Vegetarian Food", "maxResultCount": 5, "searchAlongRouteParameters": { "polyline": { "encodedPolyline": "ROUTE_POLYLINE" } }, "routingParameters": { "origin": { "latitude": 37.56617, "longitude": -122.30870 }, "travelMode":"DRIVE", "routeModifiers": { "avoidTolls": 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'
The following image shows a map that contains the route polyline, the new origin (light-blue pin), and the places in the search results (green pins). Notice how all the results are along the route but past San Mateo: