简体   繁体   English

在Google Places API中将自动完成搜索限制为“印度”

[英]Restrict Autocomplete search to “India” in Google Places API

I am using the Google Place Autocomplete API to search for cities in India. 我正在使用Google Place Autocomplete API来搜索印度的城市。 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") . 当我将国家/地区设置为.setCountry("UK")时,相同的代码可以正常工作。 What am I doing wrong? 我究竟做错了什么? Is there an alternate method for restricting the autocomplete? 是否有其他方法可以限制自动完成功能?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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