How to get Country Specific Results in Google Places API for AutocompletePredictions in Android?

12,765

Solution 1

AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().
                    setTypeFilter(Place.TYPE_COUNTRY).setCountry("IN").build();

Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                            .setFilter(typeFilter)
                            .build(this);

Solution 2

The (regions) type collection instructs the Places service to return any result matching the following types:

  • locality
  • sublocality
  • postal_code
  • country
  • administrative_area_level_1
  • administrative_area_level_2

The (regions) type collection is not supported in Google Places API for Android.

Reference from : Official Guide of Google Place Types.

If you want to implement this,

  • Use LatLng bounds with your country's specific area specified by latitude and longitude to restrict the results.

  • You can also use the Google Places Web Service.

Sample: For restricting your country via Web Service, showing results of country India.

URL: https://maps.googleapis.com/maps/api/place/autocomplete/json?key=AIzaSyCkvow9LlFNOywy8lzaekn-xROBZRsSFvU&input=a&types=(cities)&components=country:ind

Note: If you want to apply the country filter to Google Places API, unfortunately you have to use WebService as of now there is no Filter/Method available in Android API to restrict by country(excluding LatLng Bounds).

Solution 3

With javascript you may use Component restrictions like below code, try to rewrite for Android:

var options = {
    // bounds : boundsByCity,
     types: ["geocode"],
     componentRestrictions: { country: "IN" } 
};

//Create the search box and link it to the UI element
var input = document.getElementById('pac-input');
var temp = new google.maps.places.Autocomplete(input, options);
Share:
12,765
Chintan Shah
Author by

Chintan Shah

Programming is 10% writing code and 90% understanding why it's not working!

Updated on July 29, 2022

Comments

  • Chintan Shah
    Chintan Shah almost 2 years

    I want to get Search Results for a Specific Country. I have tried for setting Latitude and Longitude, but it may include other countries' results near to required Country!

    I have seen other questions which suggest to pass &components=country:COUNTRY_NAME in the request URL(I have seen it here). But I want to pass request from Google Places API.

    I'm using following method:

    Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint.toString(), mBounds, mPlaceFilter);

    But I couldn't find any way to pass a country name for getting result for the specific country. (I have tried passing country name in the Request URL and it is working.) But how to achieve the same by using Places.GeoDataApi.getAutocompletePredictions() method?

    Or is there any other way to achieve this without sending country name in the Request URL?

  • Chintan Shah
    Chintan Shah over 8 years
    Hi @shreyas, I have tried both. But I want to pass it from Google API. I want it because I want to show user quick response in AutoCompleteTextView while in JSON first I need to find the data from JSON and then show it as list. So better option for me is to use Google API. So is there any way to do it without using Web Services?
  • Vipul Asri
    Vipul Asri over 8 years
    Your answer doesn't provide information to implement this in android. Please post the code related to android as this question mentions it.
  • Lukas Lechner
    Lukas Lechner over 8 years
    @those who upvoted this answer: how did you implemented it in java?
  • Srikar Reddy
    Srikar Reddy almost 8 years
    How can apply a city filter?
  • Nafiz Bayındır
    Nafiz Bayındır over 7 years
    setCountry is working. Only thing that it requires com.google.android.gms:play-services-places:9.6.1. Check developers.google.com/android/guides/releases