Nearby Search (New)

Select platform: Android iOS JavaScript Web Service

A Nearby Search (New) request takes as input the region to search specified as a circle, defined by the latitude and longitude coordinates of the center point of the circle and the radius in meters. The request returns a list of matching places, each represented by a Place object, within the specified search area.

By default, the response contains places of all types within the search area. You can optionally filter the response by specifying a list of place types to explicitly include in or exclude from the response. For example, you can specify to include only those places in the response that are of type "restaurant", "bakery", and "cafe", or exclude all places of type "school".

Nearby Search (New) requests

Make a Nearby Search (New) request by calling PlacesClient.searchNearby, passing a SearchNearbyRequest object that defines the request parameters.

The SearchNearbyRequest object specifies all of the required and optional parameters for the request. The required parameters include:

  • The list of fields to return in the Place object, which is also known as a field mask. If you don't specify at least one field in the field list, or if you omit the field list, then the call returns an error.
  • The location restriction for the search area, defined as a latitude/longitude pair and radius value, in meters.

This example nearby search request specifies that the response Place objects contain the place fields Place.Field.ID and Place.Field.NAME for each Place object in the search results. It also filters the response to only return places of type "restaurant" and "cafe", but exclude places of type "pizza_restaurant" and "american_restaurant".

// Define a list of fields to include in the response for each returned place.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the search area as a 1000 meter diameter circle in New York, NY.
LatLng center = new LatLng(40.7580, -73.9855);
CircularBounds circle = CircularBounds.newInstance(center, /* radius = */ 1000);

// Define a list of types to include.
final List<String> includedTypes = Arrays.asList("restaurant", "cafe");
// Define a list of types to exclude.
final List<String> excludedTypes = Arrays.asList("pizza_restaurant", "american_restaurant");

// Use the builder to create a SearchNearbyRequest object.
final SearchNearbyRequest searchNearbyRequest =
SearchNearbyRequest.builder(/* location restriction = */ circle, placeFields)
    .setIncludedTypes(includedTypes)
    .setExcludedTypes(excludedTypes)
    .setMaxResultCount(10)
    .build());

// Call placesClient.searchNearby() to perform the search.
// Define a response handler to process the returned List of Place objects.
placesClient.searchNearby(searchNearbyRequest)
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
    });

Nearby Search (New) responses

The SearchNearbyResponse class represents the response from a search request. A SearchNearbyResponse object contains:

  • A list of Place objects that represent all matching places, with one Place object per matching place.
  • Each Place object only contains the fields defined by the field list passed in the request.

For example, in the request you defined a field list as:

// Define a list of fields to include in the response for each returned place.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

This field list means that each Place object in the response contains only the place ID and name of each matching place. You can then use the Place.getId() and Place.getName() methods to access these fields in each Place object.

For more examples of accessing data in a Place object, see Access Place object data fields.

Required parameters

Use the SearchNearbyRequest object to specify the required parameters for the search.

  • Field list

    When you request place details, you must specify the data to return in the Place object for the place as a field mask. To define the field mask, pass an array of values from Place.Field to the SearchNearbyRequest object. Field masking is a good design practice to ensure that you don't request unnecessary data, which helps to avoid unnecessary processing time and billing charges.

    Specify one or more of the following fields:

    • The following fields trigger the Nearby Search (Basic) SKU:

      Place.Field.ADDRESS_COMPONENTS, Place.Field.BUSINESS_STATUS, Place.Field.ADDRESS, Place.Field.ICON_BACKGROUND_COLOR, Place.Field.ICON_URL, Place.Field.LAT_LNG, Place.Field.PHOTO_METADATAS, Place.Field.PLUS_CODE, Place.Field.ID, Place.Field.NAME, Place.Field.TYPES, Place.Field.UTC_OFFSET, Place.Field.VIEWPORT, Place.Field.WHEELCHAIR_ACCESSIBLE_ENTRANCE
    • The following fields trigger the Nearby Search (Advanced) SKU:

      Place.Field.CURRENT_OPENING_HOURS, Place.Field.SECONDARY_OPENING_HOURS, Place.Field.PHONE_NUMBER, Place.Field.PRICE_LEVEL, Place.Field.RATING, Place.Field.OPENING_HOURS, Place.Field.USER_RATINGS_TOTAL, Place.Field.WEBSITE_URI
    • The following fields trigger the Nearby Search (Preferred) SKU:

      Place.Field.CURBSIDE_PICKUP, Place.Field.DELIVERY, Place.Field.DINE_IN, Place.Field.EDITORIAL_SUMMARY, Place.Field.RESERVABLE, Place.Field.REVIEWS, Place.Field.SERVES_BEER, Place.Field.SERVES_BREAKFAST, Place.Field.SERVES_BRUNCH, Place.Field.SERVES_DINNER, Place.Field.SERVES_LUNCH, Place.Field.SERVES_VEGETARIAN_FOOD, Place.Field.SERVES_WINE, Place.Field.TAKEOUT

    To set the field list parameter, call the setPlaceFields() method when building the SearchNearbyRequest object.

    The following example defines a list of two field values to specify that the Place object returned by a request contains the Place.Field.ID and Place.Field.NAME fields:

// Define a list of fields to include in the response for each returned place.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
  • Location restriction

    A LocationRestriction object that defines the region to search specified as a circle, defined by center point and radius in meters. The radius must be between greater than 0.0 and less than or equal to 50000.0, keeping in mind that specifying a radius that is too small will return ZERO_RESULTS as a response.

    To set the location restriction parameter, call the setLocationRestriction() method when building the SearchNearbyRequest object.

Optional parameters

Use the SearchNearbyRequest object to specify the optional parameters for the search.

  • Types and primary types

    Lets you specify a list of types from types Table A used to filter the search results. Up to 50 types can be specified in each type restriction category.

    A place can only have a single primary type from types Table A associated with it. For example, the primary type might be "mexican_restaurant" or "steak_house". Use includedPrimaryTypes and excludedPrimaryTypes to filter the results on a place's primary type.

    A place can also have multiple type values from types Table A associated with it. For example a restaurant might have the following types: "seafood_restaurant", "restaurant", "food", "point_of_interest", "establishment". Use includedTypes and excludedTypes to filter the results on the list of types associated with a place.

    If a search is specified with multiple type restrictions, only places that satisfy all of the restrictions are returned. For example, if you specify includedTypes = Arrays.asList("restaurant") and excludedPrimaryTypes = Arrays.asList("steak_house"), the returned places provide "restaurant" related services but don't operate primarily as a "steak_house".

    For an example of how to use includedTypes and excludedTypes, see Nearby Search (New) requests.

    Included types

    A list of the place types from Table A to search for. If this parameter is omitted, places of all types are returned.

    To set the included types parameter, call the setIncludedTypes() method when building the SearchNearbyRequest object.

    Excluded types

    A list of place types from Table A to exclude from a search.

    If you specify both the includedTypes (such as "school") and the excludedTypes (such as "primary_school") in the request, then the response includes places that are categorized as "school" but not as "primary_school". The response includes places that match at least one of the includedTypes and none of the excludedTypes.

    If there are any conflicting types, such as a type appearing in both includedTypes and excludedTypes, an INVALID_REQUEST error is returned.

    To set the excluded types parameter, call the setExcludedTypes() method when building the SearchNearbyRequest object.

    Included primary types

    A list of primary place types from Table A to include in a search.

    To set the included primary types parameter, call the setIncludedPrimaryTypes() method when building the SearchNearbyRequest object.

    Excluded primary types

    A list of primary place types from Table A to exclude from a search.

    If there are any conflicting primary types, such as a type appearing in both includedPrimaryTypes and excludedPrimaryTypes, an INVALID_ARGUMENT error is returned.

    To set the excluded primary types parameter, call the setExcludedPrimaryTypes() method when building the SearchNearbyRequest object.

  • Maximum result count

    Specifies the maximum number of place results to return. Must be between 1 and 20 (default) inclusive.

    To set the maximum result count parameter, call the setMaxResultCount() method when building the SearchNearbyRequest object.

  • Rank preference

    The type of ranking to use. If this parameter is omitted, results are ranked by popularity. May be one of the following:

    • POPULARITY (default) Sorts results based on their popularity.
    • DISTANCE Sorts results in ascending order by their distance from the specified location.

    To set the rank preference parameter, call the setRankPreference() method when building the SearchNearbyRequest object.

  • Region code

    The region code used to format the response, specified as a two-character CLDR code value. There is no default value.

    If the country name of the formattedAddress field in the response matches the regionCode, the country code is omitted from formattedAddress.

    Most CLDR codes are identical to ISO 3166-1 codes, with some notable exceptions. For example, the United Kingdom's ccTLD is "uk" (.co.uk) while its ISO 3166-1 code is "gb" (technically for the entity of "The United Kingdom of Great Britain and Northern Ireland"). The parameter can affect results based on applicable law.

    To set the region code parameter, call the setRegionCode() method when building the SearchNearbyRequest object.

Display attributions in your app

When your app displays information obtained from PlacesClient, such as photos and reviews, the app must also display the required attributions.

For more information, see Policies for Places SDK for Android.