22

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?

K.K
  • 2,647
  • 1
  • 26
  • 32

6 Answers6

23

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

Shailesh
  • 2,116
  • 4
  • 28
  • 48
12

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);
Malek Hijazi
  • 4,112
  • 1
  • 26
  • 31
  • how can we give 2 countries in this. I need to restrict the place for 2 countries india and UAE. Is it possible ? – Jayesh M Nov 01 '19 at 09:45
8

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

PunitD
  • 2,293
  • 1
  • 20
  • 29
  • Cool!! Happy Coding :) – PunitD Oct 29 '15 at 06:42
  • @K.K any reason for removing it as answer to your question? – PunitD Nov 10 '15 at 07:20
  • Hi, sorry I didn't comment the same time as I removed the answer. The problem is: you can't use the Google Places Web Service API on a mobile app. It can only be used on a web server because you need a server API key for that. Any alternatives? – K.K Nov 10 '15 at 08:10
  • @K.K AFAIK you could use the Google Places Web Service API on mobile app..you just need to select browser key while generating the api key and that should work.. – PunitD Nov 10 '15 at 09:27
  • The browser key, if saved on the mobile app, can be compromised. That is why when you create the Browser key, Google asks you to add HTTP referrers/ websites and warns you: "Other applications might be able to use this key." if you don't add the referrers. Correct me if wrong. – K.K Nov 10 '15 at 09:32
  • 1
    @K.K yes, you are absolutely right..that's the reason Google Places API for Android has been developed..But as discussed the places api for android just provides LatLngBounds approach to restrict the result..:( Even I am looking out for the best approach to be used in such scenario.. – PunitD Nov 10 '15 at 09:38
  • Looks like Uber and Lyft use the browser key for using Google maps/places API, but I am not sure how they are making it secure on the mobile app. – K.K Nov 14 '15 at 07:12
  • @K.K are you sure? Any source? – PunitD Nov 14 '15 at 08:16
  • And according to me the best approach is to use Google Places API for Android signed with Android Key..because you can ensure that no attacker is able to use your API key(in case he gets it by debugging our apk) unless he has access to your app's signed private key.. – PunitD Nov 14 '15 at 08:27
  • Yes, I am sure. You can use the browser key/ server key to make calls to Google Places Web Service API, and compare it with Uber's autocomplete results. :) It's the same. I'll be enquiring around and will let you know. – K.K Nov 14 '15 at 08:53
  • The bounds you gave for the USA are bad - for example, you chose the southwest corner for California, but since it's a rectangle this excludes most of California and most of Texas, not to mention Hawaii and Alaska. – yuval Dec 23 '15 at 02:15
  • @yuval yup,I know..that's why I mentioned in the answer that `"above bounds would be purely rectangle and it is not possible to restrict the results to only USA using these bounds."` In fact you could change lat long to include those states but as said that would also include results apart from USA due to rectangular bounds.. – PunitD Dec 23 '15 at 06:03
3

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();
Devinder Jhinjer
  • 645
  • 7
  • 13
1

Alternatively you can just use:

yourFragment.setCountry("Country Alpha 2 code");

kunene
  • 11
  • 3
0
    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")
GilbertS
  • 591
  • 8
  • 12