如何仅在范围内获取Places.GeoDataApi.getAutocompletePredictions? [英] How to get Places.GeoDataApi.getAutocompletePredictions ONLY inside the bounds?

查看:184
本文介绍了如何仅在范围内获取Places.GeoDataApi.getAutocompletePredictions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android上的Places.GeoDataApi,并且根据执行请求的设备的位置,我会得到不同的搜索结果。我需要结果始终位于边界内。我看不到可以在getAutocompletePredictions请求中设置的位置。我缺少什么吗?

I am using Places.GeoDataApi for Android and I get different search results depending on the location of the device performing the request. I need the results to be consistently located inside the bounds. I don't see where that could be setup in the getAutocompletePredictions request. Is there anything I am missing?

我可以通过以下方式使用GoogleApiClient和Places API获得地址/位置自动完成建议:

I get address/place autocomplete suggestions using the GoogleApiClient and Places API through:

Places.GeoDataApi.getAutocompletePredictions()

该方法要求一个GoogleApiClient对象,一个用于自动完成的String以及一个用于限制搜索范围的LatLngBounds对象。这是我的用法:

The method requires a GoogleApiClient object, a String to autocomplete, and a LatLngBounds object to limit the search range. This is what my usage looks like:

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

Version in use: com.google.android.gms:play-services-location:8.3.0

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

推荐答案

好消息。截至2018年4月,Google增加了指定如何处理自动完成预测中的界限的可能性。现在,您可以使用 GeoDataClient 类的 getAutocompletePredictions()方法和 boundsMode 参数

Good news. As of April 2018 Google added possibility to specify how to treat the bounds in autocomplete predictions. Now you can use the getAutocompletePredictions() method of GeoDataClient class with boundsMode parameter

public Task< AutocompletePredictionBufferResponse> getAutocompletePredictions(字符串查询,LatLngBounds边界,int boundsMode,AutocompleteFilter过滤器)


boundsMode >-如何处理bounds参数。设置为STRICT时,预测将包含在提供的边界中。如果设置为BIAS,则预测会偏向提供的边界。如果bounds为null,则此参数无效。

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

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

You can modify your code to something similar to:

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

我希望这会有所帮助!

这篇关于如何仅在范围内获取Places.GeoDataApi.getAutocompletePredictions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆