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:

  1. 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.

  2. Include routingSummaries in the field mask so that the response includes the routingSummaries 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": [
    {
      "legs": [
        object (Leg)
      ]
    }
  ]
}

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 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
        }
      ]
    },
    {
      "legs": [
        {
          "duration": "562s",
          "distanceMeters": 2345
        }
      ]
    },
   
  ]
}

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.

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 to DRIVE, BICYCLE, WALK, or TWO_WHEELER. For more information on these options, see Available vehicle types for routes.

  • Use the routingParameters.routingPreference property to set the routing preference option to TRAFFIC_UNAWARE (default), TRAFFIC_AWARE, or TRAFFIC_AWARE_OPTIMAL. Each option has varying levels of data quality and latency. For more information, see Specify how and if to include traffic data.
  • Use the routingParameters.routeModifiers property to specify to avoidTolls, avoidHighways, avoidFerries, and avoidIndoor. 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'