Text Search

A Text Search returns information about a set of places based on a string — for example "pizza in New York" or "shoe stores near Ottawa" or "123 Main Street". The service responds with a list of places matching the text string and any location bias that has been set.

The service is especially useful for making ambiguous address queries in an automated system, and non-address components of the string may match businesses as well as addresses. Examples of ambiguous address queries are poorly-formatted addresses or requests that include non-address components such as business names. Requests like the first two examples might return zero results unless a location — such as region, location restriction, or location bias — is set.

"10 High Street, UK" or "123 Main Street, US" Multiple "High Street"s in the UK; multiple "Main Street"s in the US. Query doesn't return desirable results unless a location restriction is set.
"ChainRestaurant New York" Multiple "ChainRestaurant" locations in New York; no street address or even street name.
"10 High Street, Escher UK" or "123 Main Street, Pleasanton US" Only one "High Street" in the UK city of Escher; only one "Main Street" in the US city of Pleasanton CA.
"UniqueRestaurantName New York" Only one establishment with this name in New York; no street address needed to differentiate.
"pizza restaurants in New York" This query contains its location restriction, and "pizza restaurants" is a well-defined place type. It returns multiple results.
"+1 + 514-670-8700" This query contains a phone number. Google recommends including the country code in the search string to improve search quality. It returns multiple results for places associated with that phone number.

Text Search requests

A Text Search request is in the form:

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define latitude and longitude coordinates of the search area.
LatLng southWest = new LatLng(37.38816277477739, -122.08813770258874);
LatLng northEast = new LatLng(37.39580487866437, -122.07702325966572);

// Use the builder to create a SearchByTextRequest object.
final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food", placeFields)
  .setMaxResultCount(10)
  .setLocationRestriction(RectangularBounds.newInstance(southWest, northEast)).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();
    });

In this example, you:

  • Set the field list to include only Place.Field.ID and Place.Field.NAME. That means the Place objects in the response that represent each matching place only contain those two fields.

  • Use SearchByTextRequest.Builder to create a SearchByTextRequest object that defines the search.

    • Set the text query string to "Spicy Vegetarian Food".

    • Set the maximum number of result places to 10. The default and the maximum is 20.

    • Restrict the search area to the rectangle defined by latitude and longitude coordinates. No matches outside of this area are returned.

  • Add an OnSuccessListener and get the matching places from the SearchByTextResponse object.

Text Search responses

The SearchByTextResponse class represents the response from a search request. A SearchByTextResponse 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:

// Specify the list of fields to return.
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

  • Field list

    Specify which place data fields to return. Pass a list of Place.Field values specifying the data fields to return. There is no default list of returned fields in the response.

    Field lists are 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 Text Search (ID Only) SKU:

      Place.Field.ID, Place.Field.NAME
    • The following fields trigger the Text Search (Basic) SKU:

      Place.Field.WHEELCHAIR_ACCESSIBLE_ENTRANCE, 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.TYPES, Place.Field.UTC_OFFSET, Place.Field.VIEWPORT
    • The following fields trigger the Text 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 Text 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
  • Text query

    The text string on which to search, for example: "restaurant", "123 Main Street", or "best place to visit in San Francisco". The API returns candidate matches based on this string and orders the results based on their perceived relevance.

Optional parameters

Set these parameters by using methods of SearchByTextRequest.Builder. For example, to set the maximum results count, call SearchByTextRequest.Builder.setMaxResultCount().

  • Included type

    Restricts the results to places matching the specified type defined by Table A. Only one type may be specified. For example:

    • setIncludedType("bar")
    • setIncludedType("pharmacy")
  • Location bias

    Specifies an area to search. This location serves as a bias which means results around the specified location can be returned, including results outside the specified area.

    You can specify location restriction or location bias, but not both. Think of location restriction as specifying the region which the results must be within, and location bias as specifying the region that the results must be near but can be outside of the area.

    Specify the region as a rectangular Viewport or as a circle.

    • A circle is defined by center point and radius in meters. The radius must be between 0.0 and 50000.0, inclusive. The default radius is 0.0. For example:

      // Define latitude and longitude coordinates of the center of the search area.
      LatLng searchCenter = new LatLng(37.38816277477739, -122.08813770258874);
      
      // Use the builder to create a SearchByTextRequest object.
      // Set the radius of the search area to 500.0 meters.
      final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food", placeFields)
        .setMaxResultCount(10)
        .setLocationRestriction(CircularBounds.newInstance(searchCenter, 500.0)).build();
      
    • A rectangle is a latitude-longitude viewport, represented as two diagonally opposite low and high points. The low point marks the southwest corner of the rectangle, and the high point represents the northeast corner of the rectangle.

      A viewport is considered a closed region, meaning it includes its boundary. The latitude bounds must range between -90 to 90 degrees inclusive, and the longitude bounds must range between -180 to 180 degrees inclusive:

      • If low = high, the viewport consists of that single point.
      • If low.longitude > high.longitude, the longitude range is inverted (the viewport crosses the 180 degree longitude line).
      • If low.longitude = -180 degrees and high.longitude = 180 degrees, the viewport includes all longitudes.
      • If low.longitude = 180 degrees and high.longitude = -180 degrees, the longitude range is empty.
      • If low.latitude > high.latitude, the latitude range is empty.

      Both low and high must be populated, and the represented box cannot be empty. An empty viewport results in an error.

      For example, of a rectangular viewport see Text Search requests.

  • Location restriction

    Specifies an area to search. Results outside the specified area are not returned. Specify the region as a rectangular Viewport. See the description of Location bias for information on defining the Viewport.

    You can specify location restriction or location bias, but not both. Think of location restriction as specifying the region which the results must be within, and location bias as specifying the region that the results must be near but can be outside the area.

  • Maximum result count

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

  • Minimum rating

    Restricts results to only those whose average user rating is greater than or equal to this limit. Values must be between 0.0 and 5.0 (inclusive) in increments of 0.5. For example: 0, 0.5, 1.0, ... , 5.0 inclusive. Values are rounded up to the nearest 0.5. For example, a value of 0.6 eliminates all results with a rating less than 1.0.

  • Open now

    If true, return only those places that are open for business at the time the query is sent. If false, return all businesses regardless of open status. Places that don't specify opening hours in the Google Places database are returned if you set this parameter to false.

  • Price levels

    Restrict the search to places that are marked at certain price levels. The default is to select all price levels.

    Specify a list of one or more of the following integer values:

    • 1 - PRICE_LEVEL_INEXPENSIVE
    • 2 - PRICE_LEVEL_MODERATE
    • 3 - PRICE_LEVEL_EXPENSIVE
    • 4 - PRICE_LEVEL_VERY_EXPENSIVE
  • Rank preference

    Specifies how the results are ranked in the response. The API uses RELEVANCE by default when applicable. For example, for a query such as "Restaurants in New York City" then RELEVANCE is the default. For geographical queries, such as "Mountain View, CA", or other type of queries then no default is applied and the results appear in the order that they are returned by the backend.

    Values include:

    • SearchByTextRequest.RankPreference.DISTANCE: Rank results by distance.
    • SearchByTextRequest.RankPreference.RELEVANCE: Rank results by relevance.
  • Region code

    The region code used to format the response, specified as a two-character CLDR code value. This parameter can also have a bias effect on the search results. There is no default value.

    If the country name of the address field in the response matches the region code, the country code is omitted from address.

    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.

  • Strict type filtering

    Used with the include type parameter. When set to true, only places that match the specified types specified by include type are returned. When false, the default, the response can contain places that don't match the specified types.