Creating a place type filter for the Android place autocomplete API
This API seems to be pretty new, so there aren't many questions, not many tutorials here.
I followed google tutorial and got Autocomplete Android api app working in my app. Now I am trying to use AutocompleteFilter to restrict my results to a specific area or specific types of places.
There doesn't seem to be any documentation for a beginner like me explaining what to do.
The AutocompleteFilter requires a Collection object. How do I change the following values ββto integers?
https://developers.google.com/places/supported_types#table3
The issue was resolved by a google employee himself who I contacted via our Maps support portal to work.
The site types are listed here with their entire copies.
https://developers.google.com/android/reference/com/google/android/gms/location/places/Place
They can be used to filter the Android Place autocomplete API.
I can't comment, so I'll add a warning here for the answer above:
Not all constants from Place are available for filtering using AutocompleteFilter, only two are available on Android: Place.TYPE_GEOCODE
and Place.TYPE_ESTABLISHMENT
('address' appears in the documentation, but not as a constant in Place
)
If any other constant is used (for example Place.TYPE_LOCALITY
), the request will result in this confusing status error:
Status{statusCode=NETWORK_ERROR, resolution=null}
Depending on your requirement, you can create AutocompleteFilter
. This filter can then be applied to IntentBuilder
using setFilter()
.
For example:
-
If you want to limit results in a specific country, you can do this:
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().setCountry("IN").build(); Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).setFilter(typeFilter).build(this);
-
If you want to limit the results to a specific type of place, you can do it in a similar way by changing the Filter as:
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS).build();
Android Place Autocomplete does not support all the types listed by the link given in the previous answer.
Since v8.4 the supported types are provided by AutocompleteFilter as described 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;