Fotos zum Ort

Plattform auswählen: Android iOS JavaScript Webdienst

Mit dem Places SDK for Android können Sie ein Ortsfoto anfordern, das in Ihrer App angezeigt wird. Fotos, die vom Fotodienst zurückgegeben werden, stammen aus unterschiedlichen Quellen, einschließlich Fotos von Unternehmensinhabern und Nutzern. So rufen Sie ein Bild für einen Ort ab:

  1. Rufen Sie ein Place-Objekt ab. Verwenden Sie dazu entweder fetchPlace() oder findCurrentPlace(). Geben Sie in Ihrer Anfrage unbedingt das Feld PHOTO_METADATAS an.
  2. Fügen Sie im OnSuccessListener für Ihre FetchPlaceRequest ein FetchPhotoRequest hinzu und geben Sie optional die maximale Höhe und Breite in Pixeln an. Fotos dürfen maximal eine Breite oder Höhe von 1.600 px haben.
  3. Füge einen OnSuccessListener hinzu und erhalte die Bitmap vom FetchPhotoResponse.

Ort auswählen

Das folgende Beispiel zeigt, wie ein Ortsfoto abgerufen wird.

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

      

Attribution

In den meisten Fällen können Ortsfotos ohne Namensnennung verwendet werden oder die erforderliche Namensnennung ist im Bild enthalten. Wenn die zurückgegebene PhotoMetadata-Instanz jedoch eine Attribution enthält, müssen Sie die zusätzliche Attribution überall dort einfügen, wo Sie das Bild anzeigen. Weitere Informationen finden Sie unter Attributionen anzeigen.

Nutzung und Abrechnung

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