简体   繁体   English

在 Google Places Android API 中将自动完成搜索限制为特定国家/地区

[英]Restrict Autocomplete search to a particular country in Google Places Android API

My goal is to restrict the Autocomplete results from the Google Places Android API to a particular country (United States) only.我的目标是将来自 Google Places Android API 的自动完成结果限制为仅特定国家/地区(美国)。

I am using the following API:我正在使用以下 API:

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

Looks like LatLngBounds is supposed to do this job.看起来 LatLngBounds 应该做这项工作。

  1. If yes, then what are the values of LatLngBounds for United States?如果是,那么美国的 LatLngBounds 的值是多少?

  2. Which source did you use to get LatLngBounds for a country?您使用哪个来源获取某个国家/地区的 LatLngBounds?

  3. LatLngBounds is a rectange - so it can include other countries in the result as well. LatLngBounds 是一个矩形 - 因此它也可以在结果中包含其他国家/地区。 What about that?那个怎么样?

  4. Is there a better way to do it?有没有更好的方法来做到这一点?

Starting from Google Play Services v9.6,you can now set a country filter to your autocomplete suggestions.从 Google Play Services v9.6 开始,您现在可以为自动完成建议设置国家/地区过滤器。 Please make sure your Play Services version is at least 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);  

You can change the country codes by using their ISO 'Aplha 2' Codes您可以使用ISO 'Aplha 2' 代码更改国家/地区代码

If you are using PlaceAutocompleteFragment you can set it like this:如果您使用PlaceAutocompleteFragment,您可以这样设置:

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

mAutocompleteFragment.setFilter(autocompleteFilter);

LatLngBounds constructor method definition looks like below: LatLngBounds 构造函数方法定义如下所示:

LatLngBounds (LatLng southwest, LatLng northeast)   

ie it takes two LatLng values one for southwest location of boundary and other is northeast location of the boundary and the other two points of the rectangle are trivially computed.即它需要两个 LatLng 值,一个是边界的西南位置,另一个是边界的东北位置,矩形的另外两个点是简单计算的。

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

my rough guess if i am not wrong, southwest LatLng values would be 32.6393,-117.004304 and northeast LatLng values would be 44.901184 ,-67.32254 for USA.我的粗略猜测,如果我没记错的话,美国的西南 LatLng 值为32.6393,-117.004304 ,东北部 LatLng 值为44.901184,-67.32254

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

I generally use http://www.findlatitudeandlongitude.com/ to check those LatLng values by putting marker at required locations.我通常使用http://www.findlatitudeandlongitude.com/通过在所需位置放置标记来检查这些 LatLng 值。

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

Yes, you are correct the above bounds would be purely rectangle and it is not possible to restrict the results to only USA using these bounds.是的,您是正确的,上面的边界将是纯粹的矩形,并且不可能使用这些边界将结果仅限于美国。
This is because the rectangle[bounds] would be calculated using two Latlng points[SE & NW] we provide to constructer and other two points would be calculated trivially.这是因为矩形 [边界] 将使用我们提供给构造函数的两个纬度点 [SE & NW] 计算,而其他两个点将被简单计算。

Is there a better way to do it?

Yes, you could use Google Places Web Service API instead.Below tutorial explains how to use the API..是的,您可以改用 Google Places Web Service API。下面的教程解释了如何使用 API。
http://codetheory.in/google-place-api-autocomplete-service-in-android-application/ http://codetheory.in/google-place-api-autocomplete-service-in-android-application/

You could use address types and address components to restrict the results using this API in the way you desire: https://developers.google.com/maps/documentation/geocoding/intro#Types您可以使用地址类型和地址组件以您想要的方式限制使用此 API 的结果: https : //developers.google.com/maps/documentation/geocoding/intro#Types

You could restrict by country,region and various other types using the types mentioned in the link.您可以使用链接中提到的类型按国家、地区和各种其他类型进行限制。

Hope it helps!!希望能帮助到你!!

PS: If someone could answer how to restrict using Bounds in Play Services API to particular country it would be really great!! PS:如果有人能回答如何限制在特定国家/地区使用 Bounds in Play Services API,那就太好了!!

For new Place api 2019, You have to do this对于新的 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.

相关问题 在Google Places API中将自动完成搜索限制为“印度” - Restrict Autocomplete search to “India” in Google Places API 将地方自动填充API限制为特定国家/地区Android的城市 - Restrict Places Autocomplete API to cities of specific country android 如何限制谷歌地方自动填充到特定国家/地区? - 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.)? 如何在Android中使用Google Places API限制特定位置? - How to Use google places api in android with restrict particular place? Android:如何使用Google Places API(自动填充)搜索附近的酒店? - Android: How to search for nearby hotels using the Google Places API (Autocomplete)? 适用于Android的Google Places API自动完成提取数据 - Google Places API for Android autocomplete extract data 具有Titanium的Android的Google Places API自动完成功能 - Google Places API Autocomplete for Android With Titanium Google Places API自动完成错误Android - Google Places API AutoComplete error Android Android Google商家信息自动填充功能无法正常运行 - Android Google Places autocomplete api bounds not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM