简体   繁体   中英

Restrict Autocomplete search to “India” in Google Places API

I am using the Google Place Autocomplete API to search for cities in India. I used the following code to filter the Autocomplete:

    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_CITIES)
            .setCountry("India")
            .build();
    autocompleteFragment.setFilter(typeFilter);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i("TAG", "Place: " + place.getName());
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i("TAG", "An error occurred: " + status);
        }
    });

On running this code, the autocomplete dialog shows a Can't load search results error message.

The same code works fine when I set the country as .setCountry("UK") . What am I doing wrong? Is there an alternate method for restricting the autocomplete?

.setCountry("India")更改为.setCountry("IN")写国家代码而不是国家名称...更多信息在这里

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM