选择要使用 Places API(新)客户端库返回的字段

问题描述 投票:0回答:1

Places API 客户端库(用于 GetPlace)的官方文档有客户端库的示例用法如下

import com.google.api.core.ApiFuture;
import com.google.maps.places.v1.GetPlaceRequest;
import com.google.maps.places.v1.Place;
import com.google.maps.places.v1.PlaceName;
import com.google.maps.places.v1.PlacesClient;

public class AsyncGetPlace {

  public static void main(String[] args) throws Exception {
    asyncGetPlace();
  }

  public static void asyncGetPlace() throws Exception {
    // This snippet has been automatically generated and should be regarded as a code template only.
    // It will require modifications to work:
    // - It may require correct/in-range values for request initialization.
    // - It may require specifying regional endpoints when creating the service client as shown in
    // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
    try (PlacesClient placesClient = PlacesClient.create()) {
      GetPlaceRequest request =
          GetPlaceRequest.newBuilder()
              .setName(PlaceName.of("[PLACE_ID]").toString())
              .setLanguageCode("languageCode-2092349083")
              .setRegionCode("regionCode-1991004415")
              .setSessionToken("sessionToken-696552189")
              .build();
      ApiFuture<Place> future = placesClient.getPlaceCallable().futureCall(request);
      // Do something.
      Place response = future.get();
    }
  }
}

在哪里可以指定要返回的字段?

什么是.setLanguageCode、.setRegionCode、.setSessionToken?

我假设 .setSessionToken 用于自动完成功能,但我不确定前两个是什么。我还没有在 HTTP GET 请求用法中看到它们。

我已经在 Github 上搜索他们的文档几天了,意外地实现了旧的 Places API,并且我正在修复我的代码以使用新的 API。然而,他们的图书馆结构相当广泛,很难找到我想要找到的东西。

我也无法访问他们的客户端库文档页面,因此如果有人能够访问该页面,请告诉我。

spring google-maps web-services google-cloud-platform google-places-api
1个回答
0
投票

您还需要使用

FieldMask
指定要在 Java 的 Places API 客户端库中返回的字段。
FieldMask
类可以从
com.google.protobuf
导入,有关如何构造字段掩码的更多信息,请参阅 field_mask.proto,它也在 Places API 文档中被引用。

导入

FieldMask
类后,

import com.google.protobuf.FieldMask

将其添加到您的

GetPlaceRequest
。您可以使用
setFieldMask
方法来指定要返回的字段。像这样:

GetPlaceRequest request =
          GetPlaceRequest.newBuilder()
              .setName(PlaceName.of("[PLACE_ID]").toString())
              .setLanguageCode("languageCode-2092349083")
              .setRegionCode("regionCode-1991004415")
              .setSessionToken("sessionToken-696552189")
              .setFieldMask(FieldMask.newBuilder().addPaths("formattedAddress").build())
              .build();

至于您关于

setLanguageCode, .setRegionCode, and .setSessionToken
的其他问题,后者确实是设置在自动完成会话中使用的会话令牌。对于另外两个,

  • setLanguageCode
    - 用于指定返回结果所使用的语言。例如,
    "en"
    表示英语。
  • setRegionCode
    - 设置区域代码,它会影响结果以支持指定区域。例如,
    "US"
    代表美国。

值得注意: 每个方法(如

"languageCode-2092349083"
)括号内的值是任意占位符值,因为示例代码片段是自动生成的,旨在替换为实际值

© www.soinside.com 2019 - 2024. All rights reserved.