Search in sources :

Example 1 with PhotoMetadata

use of com.google.android.libraries.places.api.model.PhotoMetadata in project android-places-demos by googlemaps.

the class PlacePhotosActivity method getPlacePhoto.

private void getPlacePhoto() {
    // [START maps_places_get_place_photos]
    // 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(// Optional.
        500).setMaxHeight(// Optional.
        300).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.
            }
        });
    });
// [END maps_places_get_place_photos]
}
Also used : PhotoMetadata(com.google.android.libraries.places.api.model.PhotoMetadata) List(java.util.List) Bitmap(android.graphics.Bitmap) ImageView(android.widget.ImageView) PlacesClient(com.google.android.libraries.places.api.net.PlacesClient) Place(com.google.android.libraries.places.api.model.Place) PhotoMetadata(com.google.android.libraries.places.api.model.PhotoMetadata) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ApiException(com.google.android.gms.common.api.ApiException) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Collections(java.util.Collections) Log(android.util.Log) FetchPhotoRequest(com.google.android.libraries.places.api.net.FetchPhotoRequest) FetchPlaceRequest(com.google.android.libraries.places.api.net.FetchPlaceRequest) Bitmap(android.graphics.Bitmap) FetchPhotoRequest(com.google.android.libraries.places.api.net.FetchPhotoRequest) Place(com.google.android.libraries.places.api.model.Place) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

Bitmap (android.graphics.Bitmap)1 Log (android.util.Log)1 ImageView (android.widget.ImageView)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 ApiException (com.google.android.gms.common.api.ApiException)1 PhotoMetadata (com.google.android.libraries.places.api.model.PhotoMetadata)1 Place (com.google.android.libraries.places.api.model.Place)1 FetchPhotoRequest (com.google.android.libraries.places.api.net.FetchPhotoRequest)1 FetchPlaceRequest (com.google.android.libraries.places.api.net.FetchPlaceRequest)1 PlacesClient (com.google.android.libraries.places.api.net.PlacesClient)1 Collections (java.util.Collections)1 List (java.util.List)1