gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:16 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com