Puoi utilizzare l'SDK Places for Android per richiedere una foto del luogo da mostrare nella tua applicazione. Le foto restituite dal servizio foto provengono da varie fonti, tra cui i proprietari di attività e le foto fornite dagli utenti.
Scegli il formato della foto
Places SDK for Android supporta due formati per la foto richiesta:
- Tutte le versioni dell'SDK Places per Android: restituisce l'immagine bitmap. L'immagine bitmap ha una dimensione massima di 1600 x 1600 pixel.
- SDK Places per Android (nuova) versione 3.4 e successive: restituisce un URI per l'immagine bitmap. Le dimensioni massime dell'immagine bitmap sono pari a 4800 x 4800 pixel.
Procedura di recupero delle foto
Per recuperare un'immagine per un luogo:
- Utilizza Place Details (Dettagli posizione) per recuperare un oggetto
Place
(utilizzafetchPlace()
ofindCurrentPlace()
). Assicurati di includere il campoPlace.Field PHOTO_METADATAS
nell'elenco dei campi da includere nell'oggettoPlace
della risposta. - In
OnSuccessListener
perFetchPlaceResponse
oFindCurrentPlaceResponse
:- Utilizza
Place.getPhotoMetadas()
per ottenere l'oggetto metadati foto, di tipoPhotoMetadata
dall'oggettoPlace
risposta. - Per ottenere un'immagine bitmap:
- Crea un oggetto
FetchPhotoRequest
, specificando facoltativamente l'altezza e la larghezza massime (in pixel). Le foto possono avere una larghezza o un'altezza massima di 1600 px. - Utilizza
PlacesClient.fetchPhoto()
per richiedere la bitmap della foto. - Aggiungi un
OnSuccessListener
e acquisisci la foto daFetchPhotoResponse
.
- Crea un oggetto
- Per ottenere l'URI di una foto:
- Crea un oggetto
FetchResolvedPhotoUriRequest
per effettuare la richiesta. Le foto possono avere una larghezza o un'altezza massima di 4800 px. - Utilizza
PlacesClient.fetchResolvedPhotoUri()
per richiedere l'URI della foto. - Aggiungi un valore
OnSuccessListener
e recupera l'URI della foto dall'oggettoFetchResolvedPhotoUriResponse
.
- Crea un oggetto
- Utilizza
Accedere ai dati di PhotoMetadata aggiunti nella versione 3.3.0 e successive
L'SDK Places per Android (novità) aggiunge il campo AuthorAttributions
alla classe PhotoMetadata
. Se la tua app attiva il nuovo SDK, l'oggetto PhotoMetadata
restituito da Place.getPhotoMetadas()
può contenere una o più attribuzioni dell'autore.
Quando l'oggetto PhotoMetadata
contiene attribuzioni, le nuove attribuzioni degli autori aggiunte nella versione 3.3.0 o quelle esistenti disponibili nella versione 3.2.0 e precedenti, devi mostrarle insieme alla foto. Per ulteriori informazioni sulla gestione di tutti i tipi di
attribuzioni, consulta la sezione Attribuzioni.
Per completare l'oggetto PhotoMetadata
con le attribuzioni dell'autore, devi:
- Abilita il nuovo SDK quando configuri il progetto Google Cloud.
- Inizializza il nuovo SDK all'interno di un'attività o di un frammento.
- Includi
Place.Field.PHOTO_METADATAS
nell'elenco dei campi della richiesta dei dettagli del luogo. - Chiama
PlacesClient.fetchPlace()
per ottenere l'oggettoPlace
ePlace.getPhotoMetadas()
per ottenere l'oggettoPhotoMetadata
. Il campo delle attribuzioni dell'autore non è supportato daPlacesClient.findCurrentPlace()
. - Utilizza
PhotoMetadata.getAuthorAttributions()
per ottenere le attribuzioni degli autori.
Scatta una foto
In questa sezione viene descritto come recuperare una foto come bitmap o come URI.
Ottieni una foto di un luogo come bitmap
L'esempio seguente mostra come ottenere una foto di un luogo come bitmap.
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. } }); });
Ottieni l'URI di una foto di un luogo
L'esempio seguente mostra come ottenere l'URI di una foto di un luogo.
// 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 FetchResolvedPhotoUriRequest. final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) // Optional. .setMaxHeight(300) // Optional. .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. } }); });
Attribuzioni
Nella maggior parte dei casi, le foto dei luoghi possono essere utilizzate senza attribuzione o avranno
l'attribuzione richiesta inclusa nell'immagine. Tuttavia, l'oggetto metadati foto, di tipo PhotoMetadata
, può contenere uno di due tipi di attribuzioni aggiuntive:
- Attributions, una stringa di attribuzione a cui accede
PhotoMetadata.getAttributions()
. - AuthorAttributions, un oggetto
AuthorAttributions
a cui accedePhotoMetadata.getAuthorAttributions()
.
Se l'oggetto PhotoMetadata
restituito include uno di questi due tipi di attribuzione, devi includere l'attribuzione nell'applicazione ovunque mostri l'immagine. Per ulteriori informazioni, consulta la sezione Visualizzazione delle attribuzioni.
Utilizzo e fatturazione
Per le chiamate a fetchPhoto()
viene addebitato uno SKU di Places Photo.
Per informazioni dettagliate, consulta la pagina Utilizzo e fatturazione.