如何使用GeoDataApi.getPlaceById?

How to use GeoDataApi.getPlaceById?

问题: 从 ID 字符串创建 Place 对象的最简洁方法是什么?

我想用硬编码 ID 制作一些 place 对象。 想想类似 Place place1 = new Place("ChIJS_zBNNbXAhURy-FuRT5ib9k") 的东西,但这当然行不通。

我遇到了 GeoDataApi.getPlaceById,但文档很混乱。

API 通常用于查找用户输入的任何内容(可选择使用自动完成),但我需要更小更清晰的东西(想想:单元测试目的)


我的研究: How to get a Place by ID

To get a place by ID, call GeoDataApi.getPlaceById, passing one or more place IDs.

The API returns a PlaceBuffer in a PendingResult. The PlaceBuffer contains a list of Place objects that match the supplied place IDs.

文档:

Place (需要的对象)

PlaceBuffer (可转换为地点)

PendingResult

getPlaceById (returns 一个 PendingResult)


我坚持的是什么: 我找不到将 PendingResult 转换为 PlaceBuffer 的优雅方法。

The API returns a PlaceBuffer in a PendingResult.

...

The final result object from a PendingResult<R> is of type R, which can be retrieved in one of two ways.

  • via blocking calls to await(), or await(long, TimeUnit), or
  • via a callback by passing in an object implementing interface ResultCallback to setResultCallback(ResultCallback).

一个回调太乱了,我找不到任何使用await学习的代码

您可以非常轻松地将 PendingResultgetPlaceById 变成 PlaceBuffer。您只需调用 await() 方法:

PendingResult<PlaceBuffer> result = Places.GeoDataApi.getPlaceById(
        mGoogleApiClient, placeId);
PlaceBuffer placeBuffer = result.await();

(如果您愿意,当然可以在一行中完成。)

https://github.com/googlesamples/android-play-places中有调用GeoDataApi.getPlaceByIDPendingResult<PlaceBuffer>.await的例子。