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

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


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


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


LatLngBounds constructor method definition looks like below:

LatLngBounds (LatLng southwest, LatLng northeast)   

i.e. it takes two LatLng values one for southwest location of boundary and other is northeast location of the boundary and the other two points of the rectangle are trivially computed.

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

my rough guess if i am not wrong, southwest LatLng values would be 32.6393,-117.004304 and northeast LatLng values would be 44.901184 ,-67.32254 for USA.

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

I generally use http://www.findlatitudeandlongitude.com/ to check those LatLng values by putting marker at required locations.

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

Yes, you are correct the above bounds would be purely rectangle and it is not possible to restrict the results to only USA using these bounds.
This is because the rectangle[bounds] would be calculated using two Latlng points[SE & NW] we provide to constructer and other two points would be calculated trivially.

Is there a better way to do it?

Yes, you could use Google Places Web Service API instead.Below tutorial explains how to use the API..
http://codetheory.in/google-place-api-autocomplete-service-in-android-application/

You could use address types and address components to restrict the results using this API in the way you desire:https://developers.google.com/maps/documentation/geocoding/intro#Types

You could restrict by country,region and various other types using the types mentioned in the link.

Hope it helps!!

P.S: If someone could answer how to restrict using Bounds in Play Services API to particular country it would be really great!!