[英]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. 我正在使用Play服务中的
PlaceAutocompleteApi
,我想要做的是将自动完成限制到特定的国家/地区和城市。 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. 我使用
AutocompleteFilter
这样做但我不知道在哪里指定我想限制的国家/地区。
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: 这样做的方法是将国家/地区作为
components
参数添加到Web服务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: 对于城市,您可以使用
administrative_area_level_3
标记:
sb.append("&components=administrative_area_level_3:bengaluru");
or alternatively the locality
tag: 或者
locality
标记:
sb.append("&components=locality:redmond");
References: 参考文献:
1. Geocoding API . 1. 地理编码API 。
2. Address Types and Component Types . 2. 地址类型和组件类型 。
3. Google Place API Autocomplete Service in Android Application . 3. Android应用中的Google Place API自动填充服务 。
Here's a workaround (hack if you will) that I used. 这是我使用过的解决方法(如果你愿意的话)。 It utilizes (exploits) the fact that when converted to string, each response item is a comma separated string.
它利用(利用)这样一个事实:当转换为字符串时,每个响应项都是逗号分隔的字符串。 Do the following steps before returning the result to
getFilter()
. 在将结果返回到
getFilter()
之前,请执行以下步骤。
ArrayList
, call it returnedResults
. ArrayList
,将其命名为returnedResults
。 returnedResults
and in each iteration get each prediction into a string array using returnedResults.get(i).getFullText(xxx).toString().split(",")
. returnedResults
和在每次迭代中使用获得的每个预测到一个字符串数组returnedResults.get(i).getFullText(xxx).toString().split(",")
ArrayList
variable, let's call it myResult
. ArrayList
变量中,我们将其myResult
。 myResult
. myResult
。 The reason for using separate variables is because you might not be able to manipulate the original response list so easily. 使用单独变量的原因是您可能无法轻松操作原始响应列表。 This approach works like a charm!
这种方法就像一个魅力!
Hope this helps. 希望这可以帮助。
Filter results by country 按国家/地区筛选结果
// Create Filter
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setCountry("AU")
.build();
// Attach to autocomplete fragment / widget
autocompleteFragment.setFilter(typeFilter);
val sb = StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON)
sb.append("?key=$API_KEY")
sb.append("&types=establishment")
sb.append("&location=$locationLat,$LocationLng")
sb.append("&radius=50000&strictbounds")
sb.append("&input=" + URLEncoder.encode(input, "utf8"))
This api will restrict the search to area. 此api将搜索限制为区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.