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

标签 android google-places-api

Google Places Autocomplete API 中使用的 LatLngBounds 对象是什么?

.. 和/或它是什么意思:

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));

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

来自这个资源: 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 的结果。如果您设置 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);

    }

关于android - GeoDataApi.getAutocompletePredictions() 中 LatLngBounds 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880117/

相关文章:

android - 从 google places API 检索地点描述

java - NetBeans w/Gradle 支持测试系统环境

java - 无法删除旧的 javaCompile 操作,可能类名已更改

android - Xamarin Android Player 在启动期间挂起

android - Phonegap Android 启动画面不显示

ios - Google places - GMSAutocompleteViewController didAutocompleteWith 全文

javascript - 将 Google Places Autocomplete pac-container 与输入字段相关联

android - 如何在 ADB 连接期间禁用电池充电?

java - AsyncTask 不断提供输出并更新我的列表

javascript - 谷歌放置自动完成小部件为每个请求生成一个新的 session key