您可以使用 Places SDK for Android 请求要在应用中显示的地点照片。照片服务返回的照片来自各种来源,包括商家所有者和用户贡献的照片。
Places SDK for Android 会返回一张最大尺寸为 1600 x 1600 像素的位图图像。
照片检索流程
要检索地点的图片,请执行以下操作:
- 使用地点详情提取
Place
对象(使用fetchPlace()
或findCurrentPlace()
)。请务必在要包含在响应Place
对象中的字段列表中添加Place.Field PHOTO_METADATAS
字段。 - 在
FetchPlaceResponse
或FindCurrentPlaceResponse
的OnSuccessListener
中,使用Place.getPhotoMetadas()
从响应Place
对象中获取类型为PhotoMetadata
的照片元数据对象。 - 创建一个
FetchPhotoRequest
对象,视需要指定最大高度和宽度(以像素为单位)。照片的宽度或高度上限为 1600 像素。 - 使用
PlacesClient.fetchPhoto()
请求照片位图。 - 添加
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
类型的照片元数据对象可以包含以下两种类型的附加提供方说明之一:
- Attributions,由
PhotoMetadata.getAttributions()
访问的归因字符串。 - AuthorAttributions 是由
PhotoMetadata.getAuthorAttributions()
访问的AuthorAttributions
对象。
如果返回的 PhotoMetadata
对象包含任一类型的提供方信息,则无论要在应用的哪个位置显示图片,您都必须添加提供方信息。如需了解详情,请参阅显示提供方说明。
使用量和结算
对于 fetchPhoto()
的调用,您需要支付地点照片 SKU 费用。如需了解详情,请参阅用量和结算页面。