简体   繁体   English

将地方自动填充API限制为特定国家/地区Android的城市

[英]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()之前,请执行以下步骤。

  1. Get the suggestions into an ArrayList , call it returnedResults . 将建议输入ArrayList ,将其命名为returnedResults
  2. Now traverse over 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(",")
  3. If the last element of this array is your country's name, insert it in a separate ArrayList variable, let's call it myResult . 如果此数组的最后一个元素是您所在国家/地区的名称,请将其插入单独的ArrayList变量中,我们将其myResult
  4. return 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.

相关问题 在 Google Places Android API 中将自动完成搜索限制为特定国家/地区 - Restrict Autocomplete search to a particular country in Google Places Android API 如何限制谷歌地方自动填充到特定国家/地区? - how to restrict google places autocomplete to specific country? 如何通过特定区域类型(国家/地区,子网站等)在Google Places API for Android中获取搜索自动填充功能? - How to get search autocomplete in Google Places API for Android only by specific region type (country, sublocality etc.)? 如何在Google Places API中针对Android中的自动完成预测获取特定国家/地区的结果? - How to get Country Specific Results in Google Places API for AutocompletePredictions in Android? 适用于Android的Google Places API-为城市获取0张照片 - Google Places API for android - getting 0 photos for cities 在Google Places API中将自动完成搜索限制为“印度” - Restrict Autocomplete search to “India” in Google Places API Google Places API 自动完成仅获取城市列表 - Google Places API Autocomplete get cities List only Kotlin Android 位置 API 自动完成 - Kotlin Android Places API Autocomplete Google在特定城市和国家/地区放置地区 - Google places localities sof particular cities and country Android Places自动完成小部件:api使用情况不清楚 - Android Places Autocomplete widget: api usage not clear
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM