【问题标题】:Restrict Autocomplete search to a particular country in Google Places Android API在 Google Places Android API 中将自动完成搜索限制在特定国家/地区
【发布时间】:2016-01-25 05:31:06
【问题描述】:

我的目标是将来自 Google Places Android API 的自动填充结果仅限于特定国家(美国)。

我正在使用以下 API:

        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                mBounds, mPlaceFilter);

看起来 LatLngBounds 应该完成这项工作。

  1. 如果是,那么美国的 LatLngBounds 值是多少?

  2. 您使用哪个来源获取某个国家/地区的 LatLngBounds?

  3. LatLngBounds 是一个矩形 - 因此它也可以在结果中包含其他国家/地区。那个怎么样?

  4. 有更好的方法吗?

【问题讨论】:

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


    【解决方案1】:

    从 Google Play Services v9.6 开始,您现在可以为自动填充建议设置国家/地区过滤器。请确保您的 Play Services 版本至少为 v9.6

    AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()
                                .setTypeFilter(Place.TYPE_COUNTRY)
                                .setCountry("US")
                                .build();
    Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                                        .setFilter(autocompleteFilter)
                                        .build(this);  
    

    您可以使用他们的ISO 'Aplha 2' Codes更改国家/地区代码

    【讨论】:

    【解决方案2】:

    如果您使用的是 PlaceAutocompleteFragment,您可以这样设置:

    AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()
                            .setTypeFilter(Place.TYPE_COUNTRY)
                            .setCountry("US")
                            .build();
    
    mAutocompleteFragment.setFilter(autocompleteFilter);
    

    【讨论】:

    • 我们怎么能在这两个国家。我需要限制印度和阿联酋 2 个国家的地点。有可能吗?
    【解决方案3】:

    LatLngBounds 构造方法定义如下:

    LatLngBounds (LatLng southwest, LatLng northeast)   
    

    即它需要两个 LatLng 值,一个用于边界的西南位置,另一个是边界的东北位置,矩形的其他两个点是简单计算的。

    If yes, then what are the values of LatLngBounds for United States?
    

    如果我没记错的话,我的粗略猜测是,西南纬度值将是 32.6393,-117.004304 美国的东北纬度值将是 44.901184 ,-67.32254

    Which source did you use to get LatLngBounds for a country?
    

    我通常使用http://www.findlatitudeandlongitude.com/ 通过在所需位置放置标记来检查这些 LatLng 值。

    LatLngBounds is a rectange - so it can include other countries in the result as well. What about that?
    

    是的,您是正确的,上述边界将是纯矩形,并且无法使用这些边界将结果限制为仅美国。
    这是因为矩形[bounds] 将使用我们提供给构造函数的两个 Latlng 点[SE & NW] 来计算,而其他两个点将被简单计算。

    Is there a better way to do it?
    

    是的,您可以改用 Google Places Web Service API。下面的教程解释了如何使用 API..
    http://codetheory.in/google-place-api-autocomplete-service-in-android-application/

    您可以使用地址类型和地址组件以您希望的方式使用此 API 来限制结果: https://developers.google.com/maps/documentation/geocoding/intro#Types

    您可以使用链接中提到的类型按国家、地区和各种其他类型进行限制。

    希望对你有帮助!!

    P.S:如果有人能回答如何将 Play Services API 中的 Bounds 使用限制在特定国家/地区,那就太好了!!

    【讨论】:

    • 酷!快乐编码:)
    • @K.K 有什么理由将其删除作为您问题的答案?
    • 嗨,对不起,我没有在删除答案的同时发表评论。问题是:您不能在移动应用上使用 Google Places Web Service API。它只能在 Web 服务器上使用,因为您需要一个服务器 API 密钥。有其他选择吗?
    • @K.K AFAIK,您可以在移动应用程序上使用 Google Places Web Service API。您只需要在生成 api 密钥时选择浏览器密钥,这应该可以工作..
    • @KK 是的,你是绝对正确的..这就是为 Android 开发 Google Places API 的原因..但正如所讨论的,用于 android 的地方 api 只是提供了 LatLngBounds 方法来限制结果..: (即使我也在寻找在这种情况下使用的最佳方法..
    【解决方案4】:

    对于新的 Place api 2019,您必须这样做

        FindAutocompletePredictionsRequest request = 
        FindAutocompletePredictionsRequest.builder()
       .setLocationBias(bounds)
       .setCountry("au")   //to set country only for e.g australia
       .setTypeFilter(TypeFilter.ADDRESS) 
       .setSessionToken(token)
       .setQuery(query)
       .build();
    

    【讨论】:

      【解决方案5】:

      或者你也可以使用:

      yourFragment.setCountry("Country Alpha 2 code");

      【讨论】:

        【解决方案6】:
            val autocompleteFragment =
                supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?
        
            // Specify the types of place data to return.
            autocompleteFragment!!
                .setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG))
                .setCountry("UG")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-22
          • 2016-04-02
          • 2017-08-05
          • 1970-01-01
          • 2020-11-08
          • 1970-01-01
          • 2015-09-03
          • 2017-08-28
          相关资源
          最近更新 更多