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

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder().
                    setTypeFilter(Place.TYPE_COUNTRY).setCountry("IN").build();

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

Answer:

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

Answer:

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

Answer:

There are 2 ways you can do this.

  1. Use latLng bounds for your country to restrict the results.(but this would be rectangular bounds. so the restriction would be approximate).

2 You should use the web service api. it works with android also.

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