Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Places Autocomplete API to cities of specific country android

I am using PlaceAutocompleteApi from Play Services, what I want to do is restrict auto-complete to specific country & city only. Eg. All cities from lets say India only. I am using AutocompleteFilter to do so but I don't know where to specify country that I want to restrict.

Here is my code

        List<Integer> autocompleteFilter = new ArrayList<>();
//        autocompleteFilter.add(Place.TYPE_COUNTRY);
//        autocompleteFilter.add(Place.TYPE_LOCALITY);


        mAdapter = new PlaceAutocompleteAdapter(mContext,R.layout.layout_location_text,
                mGoogleApiClient, BOUNDS, AutocompleteFilter.create(autocompleteFilter));
        mTxtLocation.setAdapter(mAdapter);

        mTxtLocation.setOnItemClickListener(mAutocompleteClickListener);

any thoughts on this?

like image 201
Manoj Avatar asked Jun 18 '15 07:06

Manoj


Video Answer


1 Answers

The way to do that is to add the country as the components parameter to the web service URL:

StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&components=country:in");

For cities you can use either the administrative_area_level_3 tag:

sb.append("&components=administrative_area_level_3:bengaluru");

or alternatively the locality tag:

sb.append("&components=locality:redmond");

References:

1. Geocoding API.

2. Address Types and Component Types.

3. Google Place API Autocomplete Service in Android Application.

like image 195
Y.S Avatar answered Sep 23 '22 20:09

Y.S