Foto del luogo (legacy)

Sviluppatori dello Spazio economico europeo (SEE)

Puoi utilizzare Places SDK for Android (Legacy) per richiedere una foto di un luogo da visualizzare nella tua applicazione. Le foto restituite dal servizio Photos provengono da diverse fonti, tra cui i proprietari delle attività e le foto fornite dagli utenti.

Places SDK for Android (Legacy) restituisce un'immagine bitmap con una dimensione massima di 1600 x 1600 pixel.

Procedura di recupero delle foto

Per recuperare un'immagine per un luogo:

  1. Utilizza Place Details (Legacy) per recuperare un oggetto Place (utilizzando fetchPlace()). Assicurati di includere il campo Place.Field PHOTO_METADATAS nell'elenco dei campi da includere nell'oggetto Place della risposta.
  2. In OnSuccessListener per FetchPlaceResponse, utilizza Place.getPhotoMetadas() per ottenere l'oggetto dei metadati della foto, di tipo PhotoMetadata dall'oggetto Place della risposta.
  3. 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.
  4. Utilizza PlacesClient.fetchPhoto() per richiedere la bitmap della foto.
  5. Aggiungi un OnSuccessListener e recupera la foto da FetchPhotoResponse.

Scatta una foto

L'esempio seguente mostra come ottenere una foto di un luogo:

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.
                                        }
                                    });
                });

      

Attribuzioni

Nella maggior parte dei casi, le foto dei luoghi possono essere utilizzate senza attribuzione o l'attribuzione richiesta sarà inclusa nell'immagine. Tuttavia, l'oggetto dei metadati della foto, di tipo PhotoMetadata, può contenere uno dei due tipi di attribuzioni aggiuntive:

Se l'oggetto PhotoMetadata restituito include uno dei due tipi di attribuzione, devi includere l'attribuzione nella tua applicazione ovunque mostri l'immagine. Per saperne di più, consulta Visualizzazione delle attribuzioni.

Utilizzo e fatturazione

Uno SKU Place Photos (Legacy) viene addebitato per le chiamate a fetchPhoto(). Per i dettagli, consulta la pagina Utilizzo e fatturazione.