如何调用setCountry()到AutocompleteFilter Google place API?

时间:2016-11-10 11:51:54

标签: android google-play-services google-places-api

我正在实施PlaceAutocompleteFragment以自动完成位置。我想将我的搜索限制在特定的国家/地区(就像之前我们在url中使用component = UK)。我怎样才能在下面的代码中实现这一点。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_REGIONS)
            .build();
    autocompleteFragment.setFilter(typeFilter);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            Log.i(TAG, "Place: " + place.getName());
            Log.i(TAG, "Place: " + place.getId());
        }

        @Override
        public void onError(Status status) {
            Log.i(TAG, "Place: " + status);
        }
    });

请帮助..

2 个答案:

答案 0 :(得分:9)

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

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(Place.TYPE_COUNTRY).setCountry("UK")
            .build();
    autocompleteFragment.setFilter(typeFilter);

您需要确保播放版本应该编译为com.google.android.gms:play-services:9.6.1'

答案 1 :(得分:0)

您要做的就是这个;

autocompleteFragment.setCountries("UK");

您可以在这里Google Developers Page Youtube Video

看到更多信息