GeoDataApi.getAutocompletePredictions() 中 LatLngBounds 的用途是什么?

What is the purpose of LatLngBounds in GeoDataApi.getAutocompletePredictions()?

LatLngBounds 对象在 Google 地点自动完成 API 中的用途是什么?

.. and/or :

是什么意思

biasing the results to a specific area specified by latitude and longitude bounds

?

在 Google Places Autocomplete 文档中,它说要传入 LatLngBoundsAutocompleteFilter .

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

在使用 Places Autocomplete 时,我可以看到 AutocompleteFilter 如何限制结果,比如按国家/地区。不清楚的是 LatLngBounds 是如何使用的。在示例代码中,Bounds 对象是这样的:

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

它说绑定是到山景城(加利福尼亚旧金山湾区的一个城市),但是当过滤器为空时我仍然可以得到其他国家的结果。

来自此资源: https://developers.google.com/places/android-api/autocomplete

Your app can get a list of predicted place names and/or addresses from the autocomplete service by calling GeoDataApi.getAutocompletePredictions(), passing the following parameters:

Required: A LatLngBounds object, biasing the results to a specific area specified by latitude and longitude bounds.

Optional: An AutocompleteFilter containing a set of place types, which you can use to restrict the results to one or more types of place.

假设您要搜索 Cafe The Coffee Day,如果您设置 LatLngBounds 结果将根据该位置显示。

例如,如果您在 New York 中设置 LatLngBounds 并搜索 cafe coffee day,您将看到 New York[=34= 的结果].如果您设置 SydneyLatLngBounds,您将看到 Sydney.

的结果

现在如果你想将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);

    }