Puoi utilizzare l'SDK Places per Android per richiedere la visualizzazione di una foto del luogo nella tua applicazione. Le foto restituite dal servizio foto provengono da varie fonti, tra cui i proprietari delle attività e le foto fornite dagli utenti. Per recuperare un'immagine di un luogo, devi seguire questi passaggi:
- Recupera un oggetto
Place
(utilizzafetchPlace()
ofindCurrentPlace()
). Assicurati di includere il campoPHOTO_METADATAS
nella richiesta. - Nella
OnSuccessListener
per la tuaFetchPlaceRequest
, aggiungi unaFetchPhotoRequest
, specificando facoltativamente l'altezza e la larghezza massime (in pixel). Le foto possono avere larghezza o altezza massime di 1600 px. - Aggiungi un tag
OnSuccessListener
e ottieni la bitmap daFetchPhotoResponse
.
Ottieni la foto di un luogo
L'esempio seguente mostra come ottenere una foto del luogo.
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.") } } }
Attribuzioni
Nella maggior parte dei casi, le foto dei luoghi possono essere utilizzate senza attribuzione o includeranno
l'attribuzione richiesta come parte dell'immagine. Tuttavia, se l'istanza
PhotoMetadata
restituita include un'attribuzione, devi
includere l'attribuzione aggiuntiva nella tua applicazione ogni volta che visualizzi
l'immagine. Per ulteriori informazioni, consulta la sezione
Visualizzare le attribuzioni.
Utilizzo e fatturazione
Per le chiamate a fetchPhoto()
viene addebitato uno SKU di Foto del luogo.
Consulta la pagina Utilizzo e
fatturazione per i dettagli.