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

Exception or error:

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?

How to solve:

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!!

###

For new Place api 2019, You have to do this

    FindAutocompletePredictionsRequest request = 
    FindAutocompletePredictionsRequest.builder()
   .setLocationBias(bounds)
   .setCountry("au")   //to set country only for e.g australia
   .setTypeFilter(TypeFilter.ADDRESS) 
   .setSessionToken(token)
   .setQuery(query)
   .build();

###

    val autocompleteFragment =
        supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?

    // Specify the types of place data to return.
    autocompleteFragment!!
        .setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG))
        .setCountry("UG")

Leave a Reply

Your email address will not be published. Required fields are marked *