地点照片

请选择平台: Android iOS JavaScript 网络服务

您可以使用 Places SDK for Android 请求要在您的应用中显示地点照片。照片服务返回的照片来自各种来源,包括商家所有者和用户贡献的照片。

选择照片格式

Places SDK for Android 支持所请求照片的两种格式:

  • 所有 Places SDK for Android 版本:返回位图图像。位图图像的大小上限为 1600 x 1600 像素。
  • Places SDK for Android(新)3.4 版及更高版本:返回位图图像的 URI。位图图像的大小上限为 4800 x 4800 像素。

照片检索过程

如需检索地点的图像,请执行下列操作:

  1. 使用地点详情提取 Place 对象(使用 fetchPlace()findCurrentPlace())。请务必在响应 Place 对象中包含的字段列表中添加 Place.Field PHOTO_METADATAS 字段。
  2. FetchPlaceResponseFindCurrentPlaceResponseOnSuccessListener 中:
    1. 使用 Place.getPhotoMetadas() 从响应 Place 对象中获取类型为 PhotoMetadata 的照片元数据对象。
    2. 如需获取位图图像,请执行以下操作
      1. 创建一个 FetchPhotoRequest 对象,视需要指定最大高度和宽度(以像素为单位)。照片的宽度或高度上限为 1600 像素。
      2. 使用 PlacesClient.fetchPhoto() 请求照片位图。
      3. 添加 OnSuccessListener 并从 FetchPhotoResponse 获取照片。
    3. 如需获取照片 URI,请执行以下操作
      1. 创建 FetchResolvedPhotoUriRequest 对象以发出请求。照片的宽度或高度上限为 4800 像素。
      2. 使用 PlacesClient.fetchResolvedPhotoUri() 请求照片 URI。
      3. 添加 OnSuccessListener 并从 FetchResolvedPhotoUriResponse 对象中获取照片 URI。

访问在 3.3.0 版及更高版本中添加的 PhotoMetadata 数据

Places SDK for Android(新)向 PhotoMetadata 类添加了 AuthorAttributions 字段。如果您的应用启用了新的 SDK,则 Place.getPhotoMetadas() 返回的 PhotoMetadata 对象可以包含一个或多个作者署名。

如果 PhotoMetadata 对象包含任何提供方说明(无论是在 3.3.0 版中添加的新作者提供方说明还是 3.2.0 及更早版本中提供的现有提供方说明),您必须将其与照片一起显示。如需详细了解如何处理所有类型的归因,请参阅归因

如需使用作者署名填充 PhotoMetadata 对象,您必须执行以下操作:

  1. 设置 Google Cloud 项目时启用新 SDK。
  2. 在 activity 或 fragment 中初始化新的 SDK
  3. 在地点详情请求的字段列表中添加 Place.Field.PHOTO_METADATAS
  4. 调用 PlacesClient.fetchPlace() 可获取 Place 对象,调用 Place.getPhotoMetadas() 可获取 PhotoMetadata 对象。PlacesClient.findCurrentPlace() 不支持“作者出处”字段。
  5. 使用 PhotoMetadata.getAuthorAttributions() 获取作者署名。

获取照片

本部分介绍了如何以位图或 URI 的形式检索照片。

获取地点照片作为位图

以下示例演示了如何获取地点照片作为位图。

Kotlin



// Define a Place ID.
val placeId = "INSERT_PLACE_ID_HERE"

// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
val fields = listOf(Place.Field.PHOTO_METADATAS)

// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
val placeRequest = FetchPlaceRequest.newInstance(placeId, fields)

placesClient.fetchPlace(placeRequest)
    .addOnSuccessListener { response: FetchPlaceResponse ->
        val place = response.place

        // Get the photo metadata.
        val metada = place.photoMetadatas
        if (metada == null || metada.isEmpty()) {
            Log.w(TAG, "No photo metadata.")
            return@addOnSuccessListener
        }
        val photoMetadata = metada.first()

        // Get the attribution text.
        val attributions = photoMetadata?.attributions

        // Create a FetchPhotoRequest.
        val photoRequest = FetchPhotoRequest.builder(photoMetadata)
            .setMaxWidth(500) // Optional.
            .setMaxHeight(300) // Optional.
            .build()
        placesClient.fetchPhoto(photoRequest)
            .addOnSuccessListener { fetchPhotoResponse: FetchPhotoResponse ->
                val bitmap = fetchPhotoResponse.bitmap
                imageView.setImageBitmap(bitmap)
            }.addOnFailureListener { exception: Exception ->
                if (exception is ApiException) {
                    Log.e(TAG, "Place not found: " + exception.message)
                    val statusCode = exception.statusCode
                    TODO("Handle error with given status code.")
                }
            }
    }

      

Java


// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";

// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
final List<Place.Field> fields = Collections.singletonList(Place.Field.PHOTO_METADATAS);

// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
final FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);

placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
    final Place place = response.getPlace();

    // Get the photo metadata.
    final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
    if (metadata == null || metadata.isEmpty()) {
        Log.w(TAG, "No photo metadata.");
        return;
    }
    final PhotoMetadata photoMetadata = metadata.get(0);

    // Get the attribution text.
    final String attributions = photoMetadata.getAttributions();

    // Create a FetchPhotoRequest.
    final FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
        .setMaxWidth(500) // Optional.
        .setMaxHeight(300) // Optional.
        .build();
    placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
        Bitmap bitmap = fetchPhotoResponse.getBitmap();
        imageView.setImageBitmap(bitmap);
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            final ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + exception.getMessage());
            final int statusCode = apiException.getStatusCode();
            // TODO: Handle error with given status code.
        }
    });
});

      

获取地点照片 URI

以下示例演示了如何获取地点照片 URI。

// Define a Place ID.
final String placeId = "INSERT_PLACE_ID_HERE";

// Specify fields. Requests for photos must always have the PHOTO_METADATAS field.
final List<Place.Field> fields = Collections.singletonList(Place.Field.PHOTO_METADATAS);

// Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace())
final FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields);

placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
    final Place place = response.getPlace();

    // Get the photo metadata.
    final List<PhotoMetadata> metadata = place.getPhotoMetadatas();
    if (metadata == null || metadata.isEmpty()) {
        Log.w(TAG, "No photo metadata.");
        return;
    }
    final PhotoMetadata photoMetadata = metadata.get(0);

    // Get the attribution text.
    final String attributions = photoMetadata.getAttributions();

    // Create a FetchResolvedPhotoUriRequest.
    final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata)
        .setMaxWidth(500) // Optional.
        .setMaxHeight(300) // Optional.
        .build();

    // Request the photo URI
    placesClient.fetchResolvedPhotoUri(photoRequest).addOnSuccessListener((fetchResolvedPhotoUriResponse) -> {
        Uri uri = fetchResolvedPhotoUriResponse.getUri();
        RequestOptions requestOptions = new RequestOptions().override(Target.SIZE_ORIGINAL);
        Glide.with(this).load(uri).apply(requestOptions).into(imageView);
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            final ApiException apiException = (ApiException) exception;
            Log.e(TAG, "Place not found: " + exception.getMessage());
            final int statusCode = apiException.getStatusCode();
            // TODO: Handle error with given status code.
        }
    });
});

归因

在大多数情况下,使用地点照片时可以不包含提供方说明,或将必需的提供方说明作为图片的一部分。但是,PhotoMetadata 类型的照片元数据对象可以包含以下两种额外提供方说明中的任何一种:

如果返回的 PhotoMetadata 对象包含这两种类型的提供方说明,则无论您要在应用中的什么位置显示该图片,都必须包含提供方说明。如需了解详情,请参阅显示提供方说明

使用量和结算

您需要为调用 fetchPhoto() 支付地点照片 SKU 费用。如需了解详情,请参阅用量和结算页面。