2015-10-26 20 views
16

我的目標是限制來自谷歌Android的地方API自動完成結果連接到特定國家(美國)只。限制在谷歌Android的地方自動完成API搜索某一個國家

我使用下列API:

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

貌似的LatLngBounds應該做這個工作。

  1. 如果是,那麼美國LatLngBounds的值是多少?

  2. 哪個源你用得到的LatLngBounds一個國家?

  3. 的LatLngBounds是rectange - 所以它可以包括在結果的其它國家。那個怎麼樣?

  4. 有沒有更好的辦法做到這一點?

回答

5

的LatLngBounds構造方法定義看起來像下面:

LatLngBounds (LatLng southwest, LatLng northeast) 

即,它需要兩個經緯度值之一邊界的西南位置和其他是邊界的東北位置和矩形的另兩個點是平凡的計算。

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/通過把標記在需要的位置,以檢查這些經緯度值。

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? 

是的,你可以使用谷歌的地方Web服務API instead.Below教程介紹瞭如何使用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服務API中的界限到特定的國家,那將非常棒!

+0

謝謝! Web服務API工作。 –

+0

酷!快樂編碼:) – Droidwala

+0

@ K.K任何理由刪除它作爲您的問題的答案? – Droidwala

16

從Google Play Services v9.6開始,您現在可以將國家/地區過濾器設置爲自動填充建議。請確保您的播放服務的版本至少爲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

6

如果您正在使用PlaceAutocompleteFragment可以將其設置是這樣的:

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

mAutocompleteFragment.setFilter(autocompleteFilter); 
相關問題