Place Photos (新版) 可讓您在應用程式中加入高品質的相片內容。您可以透過 Place Photos,存取數百萬張儲存在地點介面集資料庫的相片。Place Photos 會傳回點陣圖圖片的 URI。點陣圖圖片的大小上限為 4800 x 4800 像素。
Place Photos 要求
如何擷取地點的圖片:
- 使用 Place Details (新版),使用
fetchPlace()
擷取Place
物件。請務必將Place.Field PHOTO_METADATAS
欄位包含在要納入回應Place
物件的欄位清單中。 - 在
FetchPlaceResponse
的OnSuccessListener
中呼叫Place.getPhotoMetadas()
,從回應Place
物件取得PhotoMetadata
類型的相片中繼資料物件。 - 建立
FetchResolvedPhotoUriRequest
物件以提出要求,並傳遞相片中繼資料物件,以及最大高度和/或最大寬度值。 - 使用
PlacesClient.fetchResolvedPhotoUri()
要求相片 URI。 - 新增
OnSuccessListener
,並從FetchResolvedPhotoUriResponse
物件取得相片 URI。
必要參數
FetchResolvedPhotoUriRequest
的必要參數如下:
-
相片中繼資料
要傳回的相片中繼資料物件。
-
最大高度或最大寬度
指定要傳回圖片的高度和寬度上限 (以像素為單位)。如果圖片小於指定值,系統就會傳回原始圖片。如果圖片的任一尺寸較大,系統會將圖片調整為兩個尺寸中較小者,以符合原始長寬比。高度和最大寬度屬性都接受介於 1 至 4800 之間的整數。您必須指定最大高度和/或最大寬度。
- 如要設定高度上限參數,請在建立
FetchResolvedPhotoUriRequest
物件時呼叫setMaxHeight()
方法。 - 如要設定寬度參數上限,請在建立
FetchResolvedPhotoUriRequest
物件時呼叫setMaxWidth()
方法。
- 如要設定高度上限參數,請在建立
Place Photos 範例
下例示範如何取得地點相片 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 and author attributions. final String attributions = photoMetadata.getAttributions(); final AuthorAttributions authorAttributions = photoMetadata.getAuthorAttributions(); // Create a FetchResolvedPhotoUriRequest. final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) .setMaxHeight(300) .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.getAttributions()
存取的歸因字串。 - AuthorAttributions:
PhotoMetadata.getAuthorAttributions()
存取的AuthorAttributions
物件。
如果傳回的 PhotoMetadata
物件包含任一種作者資訊,您就必須在顯示圖片的任何位置,在應用程式中加入作者資訊。詳情請參閱「顯示作者資訊」一文。