gpt4 book ai didi

google-maps - 如何仅在边界内获取 Places.GeoDataApi.getAutocompletePredictions ?

转载 作者:行者123 更新时间:2023-12-03 06:51:18 26 4
gpt4 key购买 nike

我正在使用适用于 Android 的 Places.GeoDataApi,根据执行请求的设备的位置,我会得到不同的搜索结果。我需要结果始终位于边界内。我不知道在 getAutocompletePredictions 请求中可以在哪里设置。我有什么遗漏的吗?

我通过以下方式使用 GoogleApiClient 和 Places API 获取地址/地点自动填充建议:

Places.GeoDataApi.getAutocompletePredictions()

该方法需要一个 GoogleApiClient 对象、一个用于自动完成的字符串以及一个用于限制搜索范围的 LatLngBounds 对象。这就是我的用法:

LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));


GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();


PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, "Starbucks", bounds, null);

使用的版本:com.google.android.gms:play-services-location:8.3.0

文档: https://developers.google.com/places/android/autocomplete

最佳答案

好消息。自 2018 年 4 月起,Google 增加了指定如何处理自动完成预测中的边界的可能性。现在您可以使用getAutocompletePredictions()方法GeoDataClientboundsMode 一起上课参数

public Task<AutocompletePredictionBufferResponse> getAutocompletePredictions (String query, LatLngBounds bounds, int boundsMode, AutocompleteFilter filter)

boundsMode - How to treat the bounds parameter. When set to STRICT predictions are contained by the supplied bounds. If set to BIAS predictions are biased towards the supplied bounds. If bounds is null then this parameter has no effect.

来源:https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataClient

您可以将代码修改为类似以下内容:

LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));
GeoDataClient mGeoDataClient = Places.getGeoDataClient(getBaseContext());;

Task<AutocompletePredictionBufferResponse> results =
mGeoDataClient.getAutocompletePredictions("Starbucks", bounds, GeoDataClient.BoundsMode.STRICT, null);

try {
Tasks.await(results, 60, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printStackTrace();
}

try {
AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();

Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");

// Freeze the results immutable representation that can be stored safely.
ArrayList<AutocompletePrediction> al = DataBufferUtils.freezeAndClose(autocompletePredictions);

for (AutocompletePrediction p : al) {
CharSequence cs = p.getFullText(new CharacterStyle() {
@Override
public void updateDrawState(TextPaint tp) {

}
});
Log.i(TAG, cs.toString());
}

} catch (RuntimeExecutionException e) {
// If the query did not complete successfully return null
Log.e(TAG, "Error getting autocomplete prediction API call", e);
}

希望这会有所帮助!

关于google-maps - 如何仅在边界内获取 Places.GeoDataApi.getAutocompletePredictions ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766921/

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