2016-11-30 18 views
2

什麼是Google Places自動完成API中使用的對象LatLngBoundsGeoDataApi.getAutocompletePredictions()中LatLngBounds的用途是什麼?

..和/或這是什麼意思:

偏置的結果,通過經緯度範圍

指定一個特定的區域

在Google Places自動填充文檔中,它說要通過LatLngBoundsAutocompleteFilter

PendingResult<AutocompletePredictionBuffer> result = 
    Places.GeoDataApi.getAutocompletePredictions(
     mGoogleApiClient, query, bounds, autocompleteFilter); 

在使用Places自動完成功能時,我可以看到AutocompleteFilter如何按國家來限制結果。不清楚的是如何使用LatLngBounds。在示例代碼中有這樣的邊界對象:

private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = 
        new LatLngBounds(
        new LatLng(37.398160, -122.180831), 
        new LatLng(37.430610, -121.972090)); 

它說的綁定是山景城(在舊金山灣區加利福尼亞州的一個城市),但我仍然可以得到其他國家的結果時,過濾器爲null 。

從這個資源: https://developers.google.com/places/android-api/autocomplete

您的應用程序可以通過調用GeoDataApi.getAutocompletePredictions(),通過以下參數得到自動填充服務預測地名和/或地址列表:

必需:A LatLngBounds對象,將結果偏置到由經度和緯度邊界指定的特定區域。

可選:一個自動填充過濾器包含一組地點類型,您可以使用它來將結果限制爲一種或多種地點類型。

回答

3

假設你要搜索咖啡館咖啡天,如果設置LatLngBounds結果將根據該位置顯示。

E.g如果您在紐約設置LatLngBounds和你搜索今日咖啡,你會看到紐約的結果。如果你設置LatLngBounds悉尼你會看到悉尼的結果。

現在如果你想設置LatLngBounds到你的位置,那麼你必須得到當前的位置,並根據該設置LatLngBounds

您也可以指定半徑獲取特定結果。

例如

我正在使用以下代碼來獲取當前城市的結果。

protected GoogleApiClient mGoogleApiClient; 
private PlaceAutocompleteAdapter mAdapter; 
AutoCompleteTextView autoTextViewPlace; 

mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
       .addApi(Places.GEO_DATA_API) 
       .build(); 

// I am getting Latitude and Longitude From Web API 

if((strLatitude != null && !strLatitude.trim().isEmpty()) && (strLongitude != null && !strLongitude.trim().isEmpty())){ 
     LatLng currentLatLng = new LatLng(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude)); 
     if(currentLatLng != null){ 
       setLatlngBounds(currentLatLng); 
     } 
} 

public void setLatlngBounds(LatLng center){ 

     double radiusDegrees = 0.10; 
     LatLng northEast = new LatLng(center.latitude + radiusDegrees, center.longitude + radiusDegrees); 
     LatLng southWest = new LatLng(center.latitude - radiusDegrees, center.longitude - radiusDegrees); 
     LatLngBounds bounds = LatLngBounds.builder().include(northEast).include(southWest).build(); 

     mAdapter = new PlaceAutocompleteAdapter(getActivity(), mGoogleApiClient, bounds, 
       null); 
     autoTextViewPlace.setAdapter(mAdapter); 

    } 
+1

不知道如果我的理解完全是,因爲我能看到外面的約束仍然結果。這是否意味着 - 如果在澳大利亞和紐約有兩個同名的咖啡館,那麼將LatLngBounds設置在紐約內部,但不要過濾限制國家 - 這意味着紐約咖啡廳將首先出現。但澳大利亞仍然會優先考慮呢? – gnB

+0

查看我更新的代碼,在我的情況下,它按預期工作。你有沒有設置radiusDegrees? –

+1

那裏有很好的示例代碼。我只是使用'BOUNDS_MOUNTAIN_VIEW'的示例中定義的'LatLngBounds'對象。我的要求是至少在全國範圍內搜索 - 所以當它說邊界只針對一個城市時,我擔心的是邊界對象會使搜索半徑太小。不過,即使紐約市的**紐約比薩**(更接近山景城市)的當地商業出現在美國紐約市**之前,我仍然可以看到紐約市出現。我猜這是文檔的意思*「**偏向**結果」* ..與實際限制***這些結果的概念 – gnB