Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 646
Chintan Shah Avatar asked Dec 01 '15 11:12

Chintan Shah


2 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);
like image 120
100rbh Avatar answered Oct 13 '22 01:10

100rbh


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

like image 5
Vipul Asri Avatar answered Oct 13 '22 01:10

Vipul Asri