Place Photos

Plattform auswählen: Android iOS JavaScript Webdienst

Mit dem Places SDK for Android können Sie ein Foto für einen Ort anfordern, das in Ihrer App angezeigt werden soll. Vom Fotodienst zurückgegebene Fotos stammen aus verschiedenen Quellen, z. B. von Geschäftsinhabern und von Nutzern zur Verfügung gestellten Fotos.

Das Places SDK for Android gibt ein Bitmapbild mit einer maximalen Größe von 1.600 × 1.600 Pixeln zurück.

Abruf von Fotos

So rufen Sie ein Bild für einen Ort ab:

  1. Verwenden Sie Place Details, um ein Place-Objekt abzurufen (verwenden Sie entweder fetchPlace() oder findCurrentPlace()). Nehmen Sie das Feld Place.Field PHOTO_METADATAS in die Liste der Felder auf, die in das Place-Antwortobjekt aufgenommen werden sollen.
  2. Verwende in der OnSuccessListener für dein FetchPlaceResponse oder FindCurrentPlaceResponse Place.getPhotoMetadas(), um das Fotometadatenobjekt vom Typ PhotoMetadata aus dem Antwortobjekt Place abzurufen.
  3. Erstelle ein FetchPhotoRequest-Objekt und gib optional die maximale Höhe und Breite (in Pixeln) an. Die maximale Breite oder Höhe von Fotos darf maximal 1.600 Pixel betragen.
  4. Verwenden Sie PlacesClient.fetchPhoto(), um die Bitmap des Fotos anzufordern.
  5. Fügen Sie ein OnSuccessListener hinzu und rufen Sie das Foto aus dem FetchPhotoResponse ab.

Foto aufnehmen

Das folgende Beispiel zeigt, wie ein Ortsfoto abgerufen wird:

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

      

Attribution

In den meisten Fällen können Ortsfotos ohne Quellenangabe verwendet werden bzw. sind die erforderlichen Quellenangaben bereits im Bild enthalten. Das Fotometadatenobjekt vom Typ PhotoMetadata kann jedoch zwei Arten zusätzlicher Attributionen enthalten:

Wenn das zurückgegebene PhotoMetadata-Objekt einen der beiden Attributionstypen enthält, muss die Attribution überall dort, wo das Bild zu sehen ist, in die Anwendung eingebunden werden. Weitere Informationen finden Sie unter Zuordnungen anzeigen.

Nutzung und Abrechnung

Die SKU Places Photo wird für Anrufe bei fetchPhoto() berechnet. Weitere Informationen finden Sie auf der Seite Nutzung und Abrechnung.