【问题标题】:How to get Places.GeoDataApi.getAutocompletePredictions ONLY inside the bounds?如何仅在边界内获取 Places.GeoDataApi.getAutocompletePredictions?
【发布时间】:2016-11-23 14:18:54
【问题描述】:

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

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

Places.GeoDataApi.getAutocompletePredictions()

该方法需要一个 GoogleApiClient 对象、一个用于自动完成的 String 以及一个用于限制搜索范围的 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

【问题讨论】:

标签: google-maps google-places-api google-places android-googleapiclient


【解决方案1】:

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

public Task&lt;AutocompletePredictionBufferResponse&gt; getAutocompletePredictions (String query, LatLngBounds bounds, int boundsMode, AutocompleteFilter filter)

boundsMode - 如何处理 bounds 参数。当设置为 STRICT 时,预测包含在提供的范围内。如果设置为 BIAS,则预测偏向于提供的边界。如果 bounds 为 null 则此参数无效。

来源: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);
}

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    很遗憾,无法使用 Google Places API Android 获取特定范围内的地点。

    但是,您仍然可以使用 Google Places API 网络服务来使用附近搜索。

    这里的文档: https://developers.google.com/places/web-service/search?hl=fr

    然后您应该能够在参数中设置边界,并从 JSON 响应中获取 inside 边界的位置,如此答案中所述: https://stackoverflow.com/a/32404701/5446285

    【讨论】:

    • 谢谢 Jack,我确实考虑过这种方法,但是 Google 确实在他们的文档页面中预先警告您:“Places API Web 服务和客户端库用于服务器应用程序”我的问题受到影响一个安卓客户端。自从我问了这个问题后,我让一位同事在地图中提交报告,他们回复说他们目前正在努力支持此功能,尽管还没有发布日期。
    • Android 版 Places API 的功能请求:issuetracker.google.com/issues/38188994
    猜你喜欢
    • 1970-01-01
    • 2021-09-22
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多