限制Google Places Android API自动完成搜索结果仅限于特定国家

22

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

我正在使用以下API:

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

看起来LatLngBounds应该可以完成这个任务。

  1. 如果是这样,那么美国的LatLngBounds的值是什么?

  2. 你用哪个数据源获取一个国家的LatLngBounds?

  3. LatLngBounds是一个矩形-因此它也可能包括其他国家在结果中。那怎么办?

  4. 有更好的方法吗?

6个回答

23

从Google Play服务v9.6开始,您现在可以为自动完成建议设置国家筛选器。请确保您的Play服务版本至少为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'代码更改国家代码。

这个参考链接非常有帮助:感谢Yankee: (国家代码:http://www.nationsonline.org/oneworld/country_code_list.htm) - Rakshitha Muranga Rodrigo

12

如果您正在使用PlaceAutocompleteFragment,可以这样设置:

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

mAutocompleteFragment.setFilter(autocompleteFilter);

我们如何在这里指定两个国家?我需要限制仅限印度和阿联酋这两个国家。这可行吗? - Jayesh M

8

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 你为什么把它从答案中删除了? - PunitD
据我所知,您可以在移动应用程序上使用Google Places Web Service API。您只需要在生成API密钥时选择浏览器密钥,就可以正常工作了。 - PunitD
浏览器密钥如果保存在移动应用程序中,可能会被攻击。这就是为什么在创建浏览器密钥时,Google要求您添加HTTP referrers/网站并警告您:“其他应用程序可能会使用此密钥。”如果您没有添加引荐者,请纠正我。 - K.K
1
@K.K 是的,你说得完全正确。这就是为什么开发了适用于Android的Google Places API的原因。但正如我们所讨论的那样,Android的地点API只提供了LatLngBounds方法来限制结果。:( 即使我也在寻找在这种情况下使用的最佳方法。 - PunitD
@K.K 你确定吗?有来源吗? - PunitD
显示剩余7条评论

3

针对新的2019年Place API,您需要执行以下步骤:

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

1

或者您可以直接使用:

yourFragment.setCountry("国家Alpha 2代码");


0
    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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接