您可以使用 Places SDK for Android 要求在應用程式中顯示的地點相片。相片服務傳回的相片來自各種來源,包括業主和使用者提供的相片。如要擷取地點的圖片,請按照下列步驟操作:
- 擷取
Place
物件 (使用fetchPlace()
或findCurrentPlace()
)。請務必在要求中加入PHOTO_METADATAS
欄位。 - 在
FetchPlaceRequest
的OnSuccessListener
中新增FetchPhotoRequest
,並視需要指定高度和寬度上限 (以像素為單位)。相片的寬度或高度上限為 1600 像素。 - 新增
OnSuccessListener
並從FetchPhotoResponse
取得點陣圖。
取得地點相片
以下範例說明如何取得地點相片。
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. } }); });
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.") } } }
歸因
在多數情況下,地點相片可以在不附加屬性的情況下使用,或在圖片中納入必要的作者資訊。不過,如果傳回的 PhotoMetadata
執行個體包含歸因,您必須在顯示圖片的任何位置,在應用程式中加入額外的出處。詳情請參閱「顯示歸因」一文。
用量與計費
針對向 fetchPhoto()
發出的呼叫,系統會視為「Places Photo」SKU 來計費。詳情請參閱用量與帳單頁面。