您可以使用 Places SDK for Android (舊版) 要求地點相片,並在應用程式中顯示。相片服務傳回的相片來自各種來源,包括業主和使用者提供的相片。
Places SDK for Android (舊版) 傳回的點陣圖圖片大小上限為 1600 x 1600 像素。
相片擷取程序
如要擷取地點圖片,請按照下列步驟操作:
- 使用 Place Details (舊版) 擷取
Place物件 (使用fetchPlace())。請務必在要納入回應Place物件的欄位清單中,加入Place.Field PHOTO_METADATAS欄位。 - 在
FetchPlaceResponse的OnSuccessListener中,使用Place.getPhotoMetadas()從回應Place物件取得PhotoMetadata類型的相片中繼資料物件。 - 建立
FetchPhotoRequest物件,並視需要指定高度和寬度上限 (以像素為單位)。相片的寬度或高度上限為 1600 像素。 - 使用
PlacesClient.fetchPhoto()要求相片點陣圖。 - 新增
OnSuccessListener,並從FetchPhotoResponse取得相片。
取得相片
以下範例說明如何取得地點相片:
Kotlin
// Define a Place ID. val placeId = PlaceIdProvider.getRandomPlaceId() // 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 metadata = place.photoMetadatas if (metadata == null || metadata.isEmpty()) { Log.w(TAG, "No photo metadata.") return@addOnSuccessListener } val photoMetadata = metadata.first() // Get the attribution text. val attributions = photoMetadata?.attributions binding.placePhotosAttributions.text = attributions // Create a FetchResolvedPhotoUriRequest. val photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) // Optional. .setMaxHeight(300) // Optional. .build() placesClient.fetchResolvedPhotoUri(photoRequest) .addOnSuccessListener { fetchPhotoResponse -> val photoUri = fetchPhotoResponse.uri Glide.with(this) .load(photoUri) .into(binding.placePhotosResult) }.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 = PlaceIdProvider.getRandomPlaceId(); // 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(); binding.placePhotosAttributions.setText(attributions); // Create a FetchResolvedPhotoUriRequest. final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) // Optional. .setMaxHeight(300) // Optional. .build(); placesClient .fetchResolvedPhotoUri(photoRequest) .addOnSuccessListener( (fetchPhotoResponse) -> { Glide.with(this) .load(fetchPhotoResponse.getUri()) .into(binding.placePhotosResult); }) .addOnFailureListener( (exception) -> { if (exception instanceof ApiException) { 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:
AuthorAttributions物件,由PhotoMetadata.getAuthorAttributions()存取。
如果傳回的 PhotoMetadata 物件包含任一類型的作者資訊,您每次顯示圖片時就必須在應用程式中加入作者資訊。詳情請參閱「顯示出處」。
用量與計費
針對向 fetchPhoto() 發出的呼叫,系統會視為 Place Photos (舊版) SKU 來計費。詳情請參閱「用量與計費」頁面。