Fabien Fontaine
Fabien Fontaine

Reputation: 81

Filter Google Places Results by Type in Android

I'm using Google places API, especially PlaceAutocomplete, in my Android application.

I have to filter results to only display schools.

I see this q & a that seems to be what i want but its only for web usage: Can Google Places API pull specific types of places, like schools, without a location?

I don't know how to do the same with the Android SDK...

I try this:

val typeFilter = AutocompleteFilter.Builder()
                    .setTypeFilter(Place.TYPE_SCHOOL)
                    .build()

            val intent = PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                    .setFilter(typeFilter)
                    .build(this)

            startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)

But it's not working

Any help ???

Thanks, Fab

Upvotes: 1

Views: 2172

Answers (1)

xomena
xomena

Reputation: 32158

Unfortunately, places autocomplete doesn't support detailed types in filter. If you check the documentation, you will see that only supported types are

  • TYPE_FILTER_ADDRESS
  • TYPE_FILTER_CITIES
  • TYPE_FILTER_ESTABLISHMENT
  • TYPE_FILTER_GEOCODE
  • TYPE_FILTER_NONE
  • TYPE_FILTER_REGIONS

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

The same situation with Places API autocomplete web service:

https://developers.google.com/places/web-service/autocomplete#place_types

There is very old feature request in Google issue tracker to add detailed types filter in place autocomplete. You can see this feature request here:

https://issuetracker.google.com/issues/35820774

Feel free to star it to add your vote and subscribe to notifications.

If you are interested in implementing web service calls similar to mentioned question, have a look at Java Client for Google Maps Services:

https://github.com/googlemaps/google-maps-services-java

With this library you can implement text search functionality in Java. Something like

GeoApiContext context = new GeoApiContext.Builder()
    .apiKey("AIza......")
    .build();

TextSearchRequest req = PlacesApi.textSearchQuery(context, "Springfield Elementary");
try {
    PlacesSearchResponse resp = req.type(PlaceType.SCHOOL).await();
    if (resp.results != null && resp.results.length > 0) {
        for (PlacesSearchResult r : resp.results) {
            //TODO: implement logic for places
        }
    }
} catch(Exception e) {
    Log.e(TAG, "Error getting places", e);
}

I hope this helps!

Upvotes: 3

Related Questions