将地点自动填充API限制到特定国家/地区的城市 [英] Restrict Places Autocomplete API to cities of specific country android

查看:272
本文介绍了将地点自动填充API限制到特定国家/地区的城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Play Services的 PlaceAutocompleteApi ,我想要做的是限制自动完成到特定的国家&只有城市。例如。所有的城市都只能说印度。我使用 AutocompleteFilter 来做到这一点,但我不知道要在哪里指定我想要限制的国家/地区。



这是我的代码

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

对此有何看法?

解决方案

要做到这一点的方法是将国家作为组件参数添加到Web服务URL:

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

对于城市,您可以使用 administrative_area_level_3 标签:

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

或者 locality 标签:

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

参考文献:

1。 地理编码API



2。 地址类型和组件类型
$ b

3. Android中的Google Place API自动填充服务申请


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?

解决方案

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.

这篇关于将地点自动填充API限制到特定国家/地区的城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆