Calculate routing summary

To useText Search (New) orNearby Search (New) to calculate the travel duration and distance to each place in the response:

  1. Pass theRoutingParameters parameter in the request, usingsetOrigin() 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.

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

Use Text Search (New)

In the following request, you calculate the travel duration and distance to each place in the Text Search (New) response:

// Specify the list of fields to return.finalList<Place.Field>placeFields=Arrays.asList(Place.Field.ID,Place.Field.NAME);// Define the routing parameters object and pass the routing origin.RoutingParametersroutingParameters=RoutingParameters.builder().setOrigin(toLatLng("-33.8688, 151.1957362")).build();// Use the builder to create a SearchByTextRequest object and pass the routing parameters.// Set setRoutingSummariesIncluded to true.finalSearchByTextRequestsearchByTextRequest=SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia",placeFields).setMaxResultCount(10).setRoutingParameters(routingParameters).setRoutingSummariesIncluded(true).build();// Call PlacesClient.searchByText() to perform the search.// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg 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();});

TheSearchByTextResponse class represents the response from a search request. You can callSearchByTextResponse.getRoutingSummaries() to return the list of routing summaries. ASearchByTextResponse object also contains:

  • A list ofPlace objects that represent all matching places, with onePlace object per matching place.
  • EachPlace object only contains the fields defined by the field list passed in the request.
Note: The response only contains theduration anddistanceMeters to each place. It does not contain the actual route itself. To calculatethe route, use the Routes API, passing to it the origin and location of the destination place.

Use Nearby Search

In this example, you calculate the travel duration and distance to each place inthe Nearby Search response. This example searches for restaurants in Sydney,Australia and sets the location restriction and the routing origin to the samelatitude and longitude coordinate:

// Specify the list of fields to return.finalList<Place.Field>placeFields=Arrays.asList(Place.Field.ID,Place.Field.NAME);// Define the search area as a 500 meter diameter circle in Sydney, Australia.LatLngcenter=newLatLng(-33.8688,151.1957362);CircularBoundscircle=CircularBounds.newInstance(center,/* radius = */500);// Define the routing parameters object and pass the routing origin.RoutingParametersroutingParameters=RoutingParameters.builder().setOrigin(toLatLng("-33.8688, 151.1957362")).build();// Use the builder to create a SearchNearbyRequest object and pass the routing parameters.// Set setRoutingSummariesIncluded to true.finalSearchNearbyRequestsearchNearbyRequest=SearchNearbyRequest.builder(/* location restriction = */circle,placeFields).setIncludedTypes(includedTypes).setMaxResultCount(10).setRoutingParameters(routingParameters).setRoutingSummariesIncluded(true).build();// Call PlacesClient.searchNearby() to perform the search.// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects.placesClient.searchNearby(searchNearbyRequest).addOnSuccessListener(response->{List<Place>places=response.getPlaces();List<RoutingSummary>routingSummaries=response.getRoutingSummaries();List<Leg>legs=routingSummaries.get(0).getLegs();Durationduration=legs.get(0).getDuration();});

TheSearchNearbyResponse class represents the response from a search request. You can callSearchNearbyResponse.getRoutingSummaries() to return the list of routing summaries. ASearchNearbyResponse object also contains:

  • A list ofPlace objects that represent all matching places, with onePlace object per matching place.
  • EachPlace object only contains the fields defined by the field list passed in the request.

You don't have to use the same coordinates for the location restriction and the for routing origin. For example, you set the locations restriction to the center point of Sydney to restrict 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 restricts 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, youcan control the vehicle type, as well as other options, in the search.

In the next example, you specify the travel mode asDRIVE and to avoidhighways:

// 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().setAvoidHighways(true).build();// Define the routing parameters object and pass the routing origin.RoutingParametersroutingParameters=RoutingParameters.builder().setOrigin(toLatLng("-33.8688, 151.1957362")).setTravelMode(DRIVE).setRouteModifiers(routeModifiers).build();// Use the builder to create a SearchByTextRequest object and pass the routing parameters.// Set setRoutingSummariesIncluded to true.finalSearchByTextRequestsearchByTextRequest=SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia",placeFields).setMaxResultCount(10).setRoutingParameters(routingParameters).setRoutingSummariesIncluded(true).build();// Call PlacesClient.searchByText() to perform the search.// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg 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();});

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.