Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Countries for Android AutocompleteFilter

My app have to provide a Place Autocompleter for Google APIs. But there should only display locations for selected countries. For example only addresses in US and in UK. Here the used code-snipped for the filter:

AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(Place.TYPE_COUNTRY)
            .setCountry("US")
            .build();

Is there any solution to set the method setCountry() to multiple counties? I have tried .setCountrie("US, UK") ... but this don't work.

any idea?

like image 494
Navinaut Avatar asked May 09 '17 07:05

Navinaut


3 Answers

AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()
        .setTypeFilter(Place.TYPE_COUNTRY)
        .setCountry("my|country:ph|country:sg")
        .build();

This works for me. iOS SDK set like this. That's how I know.

like image 151
Alooza Avatar answered Sep 21 '22 13:09

Alooza


A new method setCountries() has been added to the SDK.

  1. Make sure to update places dependency on gradle to the latest (I'm using 2.2.0 here)

implementation 'com.google.android.libraries.places:places:2.2.0'

  1. Use the new method, here is an example using intent:

Java

Intent intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.OVERLAY, fields)
                .setCountries(Arrays.asList("FR", "IT"))
                .build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);

Kotlin

val intent = Autocomplete.IntentBuilder(
             AutocompleteActivityMode.OVERLAY, fields)
            .setCountries(listOf("FR", "IT"))
            .build(this)
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)

Note: keep in mind autocomplete results to up to five countries according to the documentation

like image 36
Yogi Bear Avatar answered Sep 20 '22 13:09

Yogi Bear


this an old question: but for those who are still not able to get the .setCountries(Arrays.asList("DZ", "US")) and only getting the .setCountry("US") make sure to update your place api in the gradle as 12/26/2020 its version 2.4.0

implementation 'com.google.android.libraries.places:places:2.4.0'
like image 26
Ridha Rezzag Avatar answered Sep 21 '22 13:09

Ridha Rezzag