地点照片

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

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

Places SDK for Android 会返回一张最大尺寸为 1600 x 1600 像素的位图图像。

照片检索过程

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

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

获取照片

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

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

      

归因

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

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

使用量和结算

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