[英]Restrict Autocomplete search to a particular country in Google Places Android API
我的目标是将来自 Google Places Android API 的自动完成结果限制为仅特定国家/地区(美国)。
我正在使用以下 API:
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
看起来 LatLngBounds 应该做这项工作。
如果是,那么美国的 LatLngBounds 的值是多少?
您使用哪个来源获取某个国家/地区的 LatLngBounds?
LatLngBounds 是一个矩形 - 因此它也可以在结果中包含其他国家/地区。 那个怎么样?
有没有更好的方法来做到这一点?
从 Google Play Services 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' 代码更改国家/地区代码
如果您使用PlaceAutocompleteFragment,您可以这样设置:
AutocompleteFilter autocompleteFilter = new AutocompleteFilter.Builder()
.setTypeFilter(Place.TYPE_COUNTRY)
.setCountry("US")
.build();
mAutocompleteFragment.setFilter(autocompleteFilter);
LatLngBounds 构造函数方法定义如下所示:
LatLngBounds (LatLng southwest, LatLng northeast)
即它需要两个 LatLng 值,一个是边界的西南位置,另一个是边界的东北位置,矩形的另外两个点是简单计算的。
If yes, then what are the values of LatLngBounds for United States?
我的粗略猜测,如果我没记错的话,美国的西南 LatLng 值为32.6393,-117.004304 ,东北部 LatLng 值为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?
是的,您是正确的,上面的边界将是纯粹的矩形,并且不可能使用这些边界将结果仅限于美国。
这是因为矩形 [边界] 将使用我们提供给构造函数的两个纬度点 [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
您可以使用链接中提到的类型按国家、地区和各种其他类型进行限制。
希望能帮助到你!!
PS:如果有人能回答如何限制在特定国家/地区使用 Bounds in Play Services API,那就太好了!!
对于新的 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();
或者,您可以使用:
yourFragment.setCountry("Country Aplha 2 code");
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.