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

懵懂的女人 提交于 2019-12-04 14:52:27

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}  
razdi

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();
    

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!