16

I am trying to implement the new Places API for android in my app and I cant seem to figure out how to set a filter to the request. Here is my code.

LatLng southWest = new LatLng(85, -180);
LatLng northEast = new LatLng(-85, 180);
LatLngBounds bounds = new LatLngBounds(southWest, northEast);
AutocompleteFilter filter = AutocompleteFilter.create(WHAT SHOULD GO HERE?);
PendingResult result = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient,
            search.getText().toString(), bounds, filter);

As you can see I followed the documentation provided by google here but I they dont talk about how to implement the filter, after reading the API Reference I still cant figure out how to implement the API.

I need help figuring out what should go in as the parameter for AutocompleteFilter.create() for the API. I want to filter the data by cities

Thanks

TanmayP
  • 706
  • 1
  • 6
  • 12

4 Answers4

15

From the documentation:

Table 3: Types supported in place autocomplete requests

You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. If nothing is specified, all types are returned. In general only a single type is allowed. The exception is that you can safely mix the geocode and establishment types, but note that this will have the same effect as specifying no types. The supported types are:

geocode instructs the Place Autocomplete service to return only geocoding results, rather than business results. Generally, you use this request to disambiguate results where the location specified may be indeterminate. address instructs the Place Autocomplete service to return only geocoding results with a precise address. Generally, you use this request when you know the user will be looking for a fully specified address. establishment instructs the Place Autocomplete service to return only business results. the (regions) type collection instructs the Places service to return any result matching the following types: locality sublocality postal_code country administrative_area_level_1 administrative_area_level_2 the (cities) type collection instructs the Places service to return results that match locality or administrative_area_level_3.

source: https://developers.google.com/places/supported_types

code:

    List<Integer> filterTypes = new ArrayList<Integer>();
    filterTypes.add( Place.TYPE_ESTABLISHMENT );
Paul Ruiz
  • 2,396
  • 3
  • 27
  • 47
  • Hi Paul, don't you happen to know how to restrict country apart of bounding box? Thanks. – Ivan Jun 01 '15 at 13:06
  • I'm actually not sure if that's possible, but if it is I'm not aware of it. – Paul Ruiz Jun 01 '15 at 15:49
  • The type Address is not there... Which types should I use if I want to get full address results? – Marcel Jun 24 '15 at 09:20
  • @Ivan About restricting results on country and not using bounding box, you have two choices: Use the REST API or traverse through the returned suggestions and return only results where last word is the country you are interested in. Hope that helps. – Abhishek Patel Dec 14 '15 at 04:02
  • @AbhishekPatel we decided to go with second option. And it works fine. Cheers! – Ivan Jan 24 '16 at 13:58
  • @Ivan I don't know about back in 2015 but there is an option to set country request now on both REST API and Google Play Services API. There is a method on AutocompleteFilter called setCountry and if you call it with "us" as param you'll get US cities. – Nemanja Kovacevic Jan 23 '17 at 21:17
15

It works on Google Play Service 9.4.

AutocompleteFilter filter = new AutocompleteFilter.Builder()
        .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
        .build();
Andy Cheng
  • 1,167
  • 10
  • 11
3

This is how you need to set filters in your AutoComplete Place API.

    List<Integer> filters = new ArrayList<Integer>();
        filters.add(Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_1);
    mAutocompleteFilter = AutocompleteFilter.create(filters);
    mGeoLocationAdapter = new GeoLocationAdapter(this,
            android.R.layout.simple_list_item_1,
            latLngBounds,
            mAutocompleteFilter);

Only few types are supported in Android, You will get

AutocompletePredictionBuffer{status=Status{statusCode=NETWORK_ERROR, resolution=null}}

if you use unsupported types as filters.

see this link for supported filter types..

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

SureshCS50
  • 3,608
  • 1
  • 20
  • 27
3

If you use Google Play services version 8.4.0, you can use the subclass AutocompleteFilter.Builder to create the Filter.

It's easier to use since you don't have to build the list of Place types manually.

mAutocompleteFilter = AutocompleteFilter.Builder().
    setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES).build();
mGeoLocationAdapter = new GeoLocationAdapter(this,
        android.R.layout.simple_list_item_1,
        latLngBounds,
        mAutocompleteFilter);

Be aware that there are currently compatibilities issues when installing google play services version 8.4.0. If you run into this problem, you can follow this thread, but I suggest reverting to the static deprecated AutocompleteFilter.create() method if you struggle.

Community
  • 1
  • 1
JDenais
  • 2,956
  • 2
  • 21
  • 30