Mit dem Places SDK for Android (Legacy) können Sie ein Ortsfoto anfordern, das in Ihrer Anwendung angezeigt werden soll. Die vom Fotodienst bereitgestellten Fotos stammen aus unterschiedlichen Quellen, z. B. von Geschäftsinhabern oder Nutzern.
Das Places SDK for Android (Legacy) gibt ein Bitmapbild mit einer maximalen Größe von 1.600 × 1.600 Pixeln zurück.
Fotoabrufprozess
So rufen Sie ein Bild für einen Ort ab:
- Verwenden Sie Place Details (Legacy), um ein
Place-Objekt abzurufen (mitfetchPlace()). Achten Sie darauf, das FeldPlace.Field PHOTO_METADATASin die Liste der Felder aufzunehmen, die imPlace-Objekt der Antwort enthalten sein sollen. - Verwenden Sie in der
OnSuccessListenerfür IhreFetchPlaceResponsePlace.getPhotoMetadas(), um das Fotometadatenobjekt vom TypPhotoMetadataaus dem AntwortobjektPlaceabzurufen. - Erstellen Sie ein
FetchPhotoRequest-Objekt und geben Sie optional die maximale Höhe und Breite in Pixeln an. Fotos dürfen maximal 1.600 Pixel breit oder hoch sein. - Verwenden Sie
PlacesClient.fetchPhoto(), um die Bitmap des Fotos anzufordern. - Fügen Sie ein
OnSuccessListenerhinzu und rufen Sie das Foto aus demFetchPhotoResponseab.
Foto aufnehmen
Im folgenden Beispiel wird gezeigt, wie Sie ein Foto eines Orts abrufen:
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. } }); });
Attribution
In den meisten Fällen dürfen Ortsfotos ohne Quellenangabe verwendet werden bzw. sind die erforderlichen Quellenangaben bereits im Bild eingebunden. Das Fotometadatenobjekt vom Typ PhotoMetadata kann jedoch einen von zwei Arten zusätzlicher Quellenangaben enthalten:
- Attributions: Ein Attributionsstring, auf den über
PhotoMetadata.getAttributions()zugegriffen wird. - AuthorAttributions, ein
AuthorAttributions-Objekt, auf das überPhotoMetadata.getAuthorAttributions()zugegriffen wird.
Wenn das zurückgegebene PhotoMetadata-Objekt eine der beiden Arten von Quellenangabe enthält, müssen Sie die Quellenangabe immer dann in Ihre Anwendung einbinden, wenn das Bild zu sehen ist. Weitere Informationen finden Sie unter Urheberrechtshinweise anzeigen.
Nutzung und Abrechnung
Die SKU Place Photos (Legacy) wird für Aufrufe von fetchPhoto() berechnet.
Weitere Informationen finden Sie auf der Seite Nutzung und Abrechnung.