Zdjęcia miejsca

Możesz też użyć pakietu Places SDK dla Androida, by przesłać prośbę o wyświetlenie zdjęcia w aplikacji. Zdjęcia zwracane przez usługę zdjęć pochodzą z różnych źródeł, w tym z firm i od użytkowników. Aby pobrać obraz miejsca, wykonaj te czynności:

  1. Pobierz obiekt Place (użyj fetchPlace() lub findCurrentPlace()). Pamiętaj, aby w żądaniu uwzględnić pole PHOTO_METADATAS.
  2. W polu OnSuccessListener elementu FetchPlaceRequest dodaj FetchPhotoRequest, opcjonalnie określając maksymalną wysokość i szerokość (w pikselach). Maksymalna szerokość lub wysokość obrazu to 1600 pikseli.
  3. Dodaj OnSuccessListener i pobierz bitmapę z FetchPhotoResponse.

Utwórz zdjęcie miejsca

Poniższy przykład pokazuje, jak zrobić zdjęcie miejsca.

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

      

Atrybucje

W większości przypadków zdjęcia miejsca mogą być użyte bez oznaczenia lub jeśli w ich obrazie są wymagane informacje o pochodzeniu danych. Jeśli jednak zwrócone wystąpienie PhotoMetadata zawiera atrybucję, musisz uwzględnić dodatkową atrybucję w aplikacji, gdy wyświetlasz obraz. Więcej informacji znajdziesz w sekcji o wyświetlaniu atrybucji.

Wykorzystanie i płatności

Za połączenia z numerem fetchPhoto() jest obciążany kod SKU zdjęcia. Więcej informacji znajdziesz na stronie Użycie i płatności.