您可以使用 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
的照片元数据对象可以包含以下两种类型的额外归因:
- 归因,由
PhotoMetadata.getAttributions()
访问的归因字符串。 - AuthorAttributions,一个由
PhotoMetadata.getAuthorAttributions()
访问的AuthorAttributions
对象。
如果返回的 PhotoMetadata
对象包含任一类型的提供方说明,无论您在哪里显示相应图片,都必须在应用中包含提供方说明。如需了解详情,请参阅显示提供方标识。
使用量和结算
您需要为对 fetchPhoto()
的调用支付 Places Photo SKU 费用。如需了解详情,请参阅用量和结算页面。