Place Photos

Plattform auswählen: Android iOS JavaScript Webdienst

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

Fotoformat auswählen

Das Places SDK for Android unterstützt zwei Formate für das angeforderte Foto:

  • Alle Versionen des Places SDK for Android:Gibt das Bitmapbild zurück. Das Bitmapbild darf maximal 1.600 × 1.600 Pixel groß sein.
  • Places SDK for Android (New) Version 3.4 und höher:Gibt einen URI zum Bitmapbild zurück. Das Bitmapbild darf maximal 4.800 × 4.800 Pixel groß sein.

Fotoabruf

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

  1. Verwende Place Details, um ein Place-Objekt abzurufen. Verwende entweder fetchPlace() oder findCurrentPlace(). Füge das Feld Place.Field PHOTO_METADATAS in die Liste der Felder ein, die in das Place-Antwortobjekt aufgenommen werden sollen.
  2. In der OnSuccessListener für FetchPlaceResponse oder FindCurrentPlaceResponse:
    1. Verwende Place.getPhotoMetadas(), um das Fotometadatenobjekt vom Typ PhotoMetadata aus dem Antwortobjekt Place abzurufen.
    2. So rufen Sie ein Bitmapbild ab:
      1. Erstellen Sie ein FetchPhotoRequest-Objekt und geben Sie optional die maximale Höhe und Breite (in Pixeln) an. Fotos dürfen eine maximale Breite oder Höhe von 1.600 Pixeln haben.
      2. Fordern Sie mit PlacesClient.fetchPhoto() die Bitmap des Fotos an.
      3. Füge eine OnSuccessListener hinzu und rufe das Foto aus der FetchPhotoResponse ab.
    3. So rufen Sie einen Foto-URI ab:
      1. Erstellen Sie ein FetchResolvedPhotoUriRequest-Objekt für die Anfrage. Fotos dürfen eine maximale Breite oder Höhe von 4.800 Pixeln haben.
      2. Verwenden Sie PlacesClient.fetchResolvedPhotoUri(), um den Foto-URI anzufordern.
      3. Fügen Sie einen OnSuccessListener hinzu und rufen Sie den Foto-URI aus dem Objekt FetchResolvedPhotoUriResponse ab.

Auf PhotoMetadata-Daten zugreifen, die in Version 3.3.0 und höher hinzugefügt wurden

Das Places SDK for Android (New) fügt der PhotoMetadata-Klasse das Feld AuthorAttributions hinzu. Wenn Ihre App das neue SDK aktiviert, kann das von Place.getPhotoMetadas() zurückgegebene PhotoMetadata-Objekt eine oder mehrere Autoreninformationen enthalten.

Wenn das PhotoMetadata-Objekt Quellenangaben enthält – entweder die in Version 3.3.0 hinzugefügten neuen Autorenangaben oder die in Version 3.2.0 und früheren Versionen verfügbaren Quellenangaben – müssen diese zusammen mit dem Foto angezeigt werden. Weitere Informationen zur Handhabung aller Arten von Zuordnungen finden Sie unter Quellenangaben.

Um das PhotoMetadata-Objekt mit Autorenangaben zu füllen, müssen Sie:

  1. Aktivieren Sie das neue SDK, wenn Sie Ihr Google Cloud-Projekt einrichten.
  2. Initialisieren Sie das neue SDK innerhalb einer Aktivität oder eines Fragments.
  3. Fügen Sie Place.Field.PHOTO_METADATAS in die Feldliste der „Place Details“-Anfrage ein.
  4. Rufen Sie PlacesClient.fetchPlace() auf, um das Place-Objekt abzurufen, und Place.getPhotoMetadas(), um das PhotoMetadata-Objekt abzurufen. Das Feld für Quellenangaben des Autors wird von PlacesClient.findCurrentPlace() nicht unterstützt.
  5. Verwenden Sie PhotoMetadata.getAuthorAttributions(), um Quellenangaben für Autoren abzurufen.

Foto aufnehmen

In diesem Abschnitt wird beschrieben, wie Sie ein Foto als Bitmap oder als URI abrufen.

Foto von einem Ort als Bitmap abrufen

Das folgende Beispiel zeigt, wie Sie ein Ortsfoto als Bitmap abrufen.

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

      

URI für Ortsfoto abrufen

Im folgenden Beispiel wird gezeigt, wie Sie den URI eines Ortsfotos abrufen.

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

Attribution

In den meisten Fällen dürfen Ortsfotos ohne Quellenangabe verwendet werden oder die erforderlichen Quellenangaben sind Teil des Bildes. Das Metadatenobjekt für Fotos vom Typ PhotoMetadata kann jedoch zwei Arten zusätzlicher Quellenangaben enthalten:

Wenn das zurückgegebene PhotoMetadata-Objekt einen der beiden Attributionstypen enthält, müssen Sie die Quellenangabe überall dort in Ihre Anwendung aufnehmen, wo das Bild angezeigt wird. Weitere Informationen finden Sie unter Zuordnungen 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.