Combine routing summaries with search along route

You can combinerouting summary calculations withsearch along a route.In this case,Text Search (New) returns the travel duration and distance to eachplace in the response, and then from each place to the final destination of theroute.

To use Text Search (New) to calculate the routing summary along with searchalong a route:

  1. Use the Routes API to calculate a route thatreturns a route polyline in the response.

  2. UsesearchAlongRouteParameters.setPolyline() 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.

  3. When building the request object, add.setRoutingSummariesIncluded(true).

By default, Text Search (New) performs the search along the entire route:

// Define the route polyline object using the route string.EncodedPolylineencodedPolyline=EncodedPolyline.newInstance("wblcFptchVIFOd@G@EVw@Ms@dHKR}ApNA`AF~@Hf@TjAb@bBb@~@n@p@^Rd@~@Vz@HVz@nDLt@?d@Kr@c@~@mD`G?`@aEfGkCnDuChDm`@bb@[`@{GhHeEdEciBnnBkC`DkC~DaClEuKjT_Z|l@Qb@iR~_@}EzJ_AdB_Und@kAfCaOjZkg@vcAqBzD_]rr@iBlEaBxEgArD}AlG}AhHsA`IeAnH{@dIq@dJgL~iBq@rHu@vGgAtHwArHaBhHkBzG_DpJ}Nbc@iBhGkA|EgC|LcIjb@oAhG_AvDgAdDkApC_BzCiBpCsFvGii@vn@scAxlAmLjNgSzUeRjT{TzWqExEmG|FuNlMmMhLaRvPqOlNmbAl}@mFlF{PlOmJfIoElE}LtMiSbU_H`I}}@jcAwl@vp@oAbBqA~BeAhCm@tBg@fCWrBQ~BI|DaB~rBO~D[bEa@`Dm@pDaAdE{@vC_BbEkB~Def@|z@sEzHKJeS~]}K`S{\\~l@cXpe@sBpDm@bAuCxDkBrBiC~BwCtByBnAcBx@}Bt@{Bn@gh@|LaOpDeFhAoDj@aE^kVrA_E^iEr@yD~@uBr@gMjF_EnAcCh@eFr@_DRsAD}@Jsu@xCWDqIV}BCeCOyDm@cBa@_DmA}JeE_CwAsBcBiBoBuAqBmOoX{CuEkB_CoDqDkVoUoD{CeE_DkEkC_FeCqB}@sDuAoDgAeCe@cCW}CK}BDaDTeOlBcuBrYaNlBq@Dyd@rGyFt@yBb@eBf@oCnAoBlAkIpGkAp@wBbAaCt@oFdAwKjBoGxA{FbByIjC_HfB_@KmNdDuC|@uFzBcH|C{@\\[?sBv@}@VaBVoA@y@EmAQcA[w@]aBkAeAkA}BuDUKs@uAqBsCwBcCgAiAiN_MyKsJsG{GkBaBiBuA{BwAwDkBcOaHiC_AiCg@}BQcCAcBHqBVkB`@qEjAu@LgCVgAHwG@sG?mABsH^eNr@mBXy@NqBt@uAt@aBlAkAlA}BtCyApBiAdB_BxB{A`B}@j@oAf@s@PeCVcIf@gAAkAQy@YiAo@_A{@_DgEgJqM_DeEaM}PoBiCzAsBw@kAdAGVk@f@q@z@C");// Specify the list of fields to return.finalList<Place.Field>placeFields=Arrays.asList(Place.Field.ID,Place.Field.NAME);// Define the search along route parameters object and pass the polyline object.SearchAlongRouteParameterssearchAlongRouteParameters=SearchAlongRouteParameters.builder().setPolyline(encodedPolyline).build();// Use the builder to create a SearchByTextRequest object and pass the search along route parameters.finalSearchByTextRequestsearchByTextRequest=SearchByTextRequest.builder("Spicy Vegetarian Food",placeFields).setMaxResultCount(10).setSearchAlongRouteParameters(searchAlongRouteParameters).setRoutingSummariesIncluded(true).build();// Call PlacesClient.searchByText() to perform the search.// Define a response handler to process the returned List of Place objects.placesClient.searchByText(searchByTextRequest).addOnSuccessListener(response->{List<Place>places=response.getPlaces();List<RoutingSummary>routingSummaries=response.getRoutingSummaries();List<Leg>legs=routingSummaries.get(0).getLegs();Durationduration=legs.get(0).getDuration();});

The response contains objects that include lists of the requested places fields and routing summaries containing the duration and distance to travel to each place. You can callSearchByTextResponse.getRoutingSummaries() to return the list of routing summaries.

For each entry in thelegs array, Text Search (New) returns atwo-leg trip time:

  • The first leg contains the travel duration and distance from the origin to the place.

  • The second leg contains the travel duration and distance from the place to the route destination.

Note: The response only contains the travel duration andduration for each leg. It does not contain the actual route itself. To calculate the route, passthe origin, location of the place as an intermediate waypoint, and the destination into theRoutes API.

Specify the routing origin, travel mode, and route modifiers

You can modify the search and routing summary calculation by specifying therouting origin, travel mode, route modifiers, and routing preferences. Thetravel mode and route modifiers work the same as for calculating routingsummaries without specifying a route as shown in theSpecify traveloptions topic.

By default, the first leg of each result contains the distance from the origindefined by the polyline to each place. However, you can override that default byexplicitly specifying a routing origin in the request. If specified, the firstleg of all responses specifies the distances and duration from the specifiedrouting 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:

// Define the route polyline object using the route string.EncodedPolylineencodedPolyline=EncodedPolyline.newInstance("wblcFptchVIFOd@G@EVw@Ms@dHKR}ApNA`AF~@Hf@TjAb@bBb@~@n@p@^Rd@~@Vz@HVz@nDLt@?d@Kr@c@~@mD`G?`@aEfGkCnDuChDm`@bb@[`@{GhHeEdEciBnnBkC`DkC~DaClEuKjT_Z|l@Qb@iR~_@}EzJ_AdB_Und@kAfCaOjZkg@vcAqBzD_]rr@iBlEaBxEgArD}AlG}AhHsA`IeAnH{@dIq@dJgL~iBq@rHu@vGgAtHwArHaBhHkBzG_DpJ}Nbc@iBhGkA|EgC|LcIjb@oAhG_AvDgAdDkApC_BzCiBpCsFvGii@vn@scAxlAmLjNgSzUeRjT{TzWqExEmG|FuNlMmMhLaRvPqOlNmbAl}@mFlF{PlOmJfIoElE}LtMiSbU_H`I}}@jcAwl@vp@oAbBqA~BeAhCm@tBg@fCWrBQ~BI|DaB~rBO~D[bEa@`Dm@pDaAdE{@vC_BbEkB~Def@|z@sEzHKJeS~]}K`S{\\~l@cXpe@sBpDm@bAuCxDkBrBiC~BwCtByBnAcBx@}Bt@{Bn@gh@|LaOpDeFhAoDj@aE^kVrA_E^iEr@yD~@uBr@gMjF_EnAcCh@eFr@_DRsAD}@Jsu@xCWDqIV}BCeCOyDm@cBa@_DmA}JeE_CwAsBcBiBoBuAqBmOoX{CuEkB_CoDqDkVoUoD{CeE_DkEkC_FeCqB}@sDuAoDgAeCe@cCW}CK}BDaDTeOlBcuBrYaNlBq@Dyd@rGyFt@yBb@eBf@oCnAoBlAkIpGkAp@wBbAaCt@oFdAwKjBoGxA{FbByIjC_HfB_@KmNdDuC|@uFzBcH|C{@\\[?sBv@}@VaBVoA@y@EmAQcA[w@]aBkAeAkA}BuDUKs@uAqBsCwBcCgAiAiN_MyKsJsG{GkBaBiBuA{BwAwDkBcOaHiC_AiCg@}BQcCAcBHqBVkB`@qEjAu@LgCVgAHwG@sG?mABsH^eNr@mBXy@NqBt@uAt@aBlAkAlA}BtCyApBiAdB_BxB{A`B}@j@oAf@s@PeCVcIf@gAAkAQy@YiAo@_A{@_DgEgJqM_DeEaM}PoBiCzAsBw@kAdAGVk@f@q@z@C");// Specify the list of fields to return.finalList<Place.Field>placeFields=Arrays.asList(Place.Field.ID,Place.Field.NAME);// Define the routing modifiers object.RouteModifiersrouteModifiers=RouteModifiers.builder().setAvoidTolls(true).build();// Define the routing parameters object and pass the routing origin.// Set the travel mode to DRIVE.// Pass the routeModifiers object.RoutingParametersroutingParameters=RoutingParameters.builder().setOrigin(toLatLng("37.56617, -122.30870")).setTravelMode(DRIVE).setRouteModifiers(routeModifiers).build();// Define the search along route parameters object and pass the polyline object.SearchAlongRouteParameterssearchAlongRouteParameters=SearchAlongRouteParameters.builder().setPolyline(encodedPolyline).build();// Use the builder to create a SearchByTextRequest object and pass the search along route parameters.finalSearchByTextRequestsearchByTextRequest=SearchByTextRequest.builder("Spicy Vegetarian Food",placeFields).setMaxResultCount(5).setRoutingParameters(routingParameters).setSearchAlongRouteParameters(searchAlongRouteParameters).setRoutingSummariesIncluded(true).build();// Call PlacesClient.searchByText() to perform the search.// Define a response handler to process the returned List of Place objects.placesClient.searchByText(searchByTextRequest).addOnSuccessListener(response->{List<Place>places=response.getPlaces();List<RoutingSummary>routingSummaries=result.getRoutingSummaries();List<Leg>legs=routingSummaries.get(0).getLegs();Durationduration=legs.get(0).getDuration();});

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:

Search results from the update origin.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-21 UTC.