Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Autocomplete search to a particular country in Google Places Android API

Tags:

My goal is to restrict the Autocomplete results from the Google Places Android API to a particular country (United States) only.

I am using the following API:

        PendingResult<AutocompletePredictionBuffer> results =                 Places.GeoDataApi                         .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),                                 mBounds, mPlaceFilter); 

Looks like LatLngBounds is supposed to do this job.

  1. If yes, then what are the values of LatLngBounds for United States?

  2. Which source did you use to get LatLngBounds for a country?

  3. LatLngBounds is a rectange - so it can include other countries in the result as well. What about that?

  4. Is there a better way to do it?

like image 837
K.K Avatar asked Oct 26 '15 07:10

K.K


People also ask

How do I restrict Google Maps autocomplete to certain cities?

It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.

How Use Google places autocomplete Android?

The autocomplete widget is a search dialog with built-in autocomplete functionality. As a user enters search terms, the widget presents a list of predicted places to choose from. When the user makes a selection, a Place instance is returned, which your app can then use to get details about the selected place.


2 Answers

Starting from Google Play Services v9.6,you can now set a country filter to your autocomplete suggestions. Please make sure your Play Services version is at least v9.6

AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()                             .setTypeFilter(Place.TYPE_COUNTRY)                             .setCountry("US")                             .build(); Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)                                     .setFilter(autocompleteFilter)                                     .build(this);   

You can change the country codes by using their ISO 'Aplha 2' Codes

like image 153
Yankee Avatar answered Sep 17 '22 17:09

Yankee


If you are using PlaceAutocompleteFragment you can set it like this:

AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()                         .setTypeFilter(Place.TYPE_COUNTRY)                         .setCountry("US")                         .build();  mAutocompleteFragment.setFilter(autocompleteFilter); 
like image 44
Malek Hijazi Avatar answered Sep 19 '22 17:09

Malek Hijazi