Creating a place type filter for the Place Autocomplete API for Android

14,868

Solution 1

Problem was solved by a Google employee himself whom I contacted via our Maps for Work support portal.

Here's a list of Place Types with their integer counterparts.

https://developers.google.com/android/reference/com/google/android/gms/location/places/Place

These can be used to filter the Android Place Autocomplete API.

Solution 2

I cannot comment yet so I add it here a warning for the answer above:

Not all constant from Place are available for filtering with AutocompleteFilter, only two are available on Android: Place.TYPE_GEOCODE and Place.TYPE_ESTABLISHMENT ('address' appear in documentation but not as constant in Place)

If any other constant (e.g. Place.TYPE_LOCALITY) are used the request will result in this confusing status error:

Status{statusCode=NETWORK_ERROR, resolution=null}  

Solution 3

Depending on your requirement, an AutocompleteFilter can be built. This filter can then be applied to the IntentBuilder by using setFilter().

For example:

  1. If you want to restrict the results to a particular country, you could do that by:

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().setCountry("IN").build();
    
    Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).setFilter(typeFilter).build(this);
    
  2. If you want to restrict the results to a particular place type, you could do that in a similar way by changing the Filter as:

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).build();
    

Solution 4

Android Place Autocomplete does not support all the types enumerated by the link provided in the previous answer.

Since v8.4 release, the supported types are provided by AutocompleteFilter as documented here

// AutocompleteFilter
public static final int TYPE_FILTER_NONE = 0;
public static final int TYPE_FILTER_GEOCODE = 1007;
public static final int TYPE_FILTER_ADDRESS = 2;
public static final int TYPE_FILTER_ESTABLISHMENT = 34;
public static final int TYPE_FILTER_REGIONS = 4;
public static final int TYPE_FILTER_CITIES = 5;
Share:
14,868
Asim
Author by

Asim

A beginner in Android development.

Updated on June 23, 2022

Comments

  • Asim
    Asim almost 2 years

    This API seems to be fairly new so there aren't many questions about it here, nor many tutorials.

    I followed Google's tutorial and got the Autocomplete Android api working in my app. I'm now trying to use the AutocompleteFilter to restrict my results to a certain region or certain types of places.

    There seems to be little to no documentation for a beginner like me explaining what to do.

    The AutocompleteFilter requires a Collection object. How do I change the the following values into integers?

    https://developers.google.com/places/supported_types#table3