You can use the Places SDK for Android to request a place photo to display in your application. Photos returned by the photos service come from a variety of sources, including business owners and user-contributed photos. To retrieve an image for a place, you must take the following steps:
- Fetch a
Place
object (use eitherfetchPlace()
, orfindCurrentPlace()
). Be sure to include thePHOTO_METADATAS
field in your request. - In the
OnSuccessListener
for yourFetchPlaceRequest
, add aFetchPhotoRequest
, optionally specifying maximum height and width (in pixels). Photos can have a maximum width or height of 1600px. - Add an
OnSuccessListener
and get the bitmap from theFetchPhotoResponse
.
Get a place photo
The following example demonstrates getting a place photo.
// Define a Place ID. String placeId = "INSERT_PLACE_ID_HERE"; // Specify fields. Requests for photos must always have the PHOTO_METADATAS field. List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS); // Get a Place object (this example uses fetchPlace(), but you can also use findCurrentPlace()) FetchPlaceRequest placeRequest = FetchPlaceRequest.newInstance(placeId, fields); placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> { Place place = response.getPlace(); // Get the photo metadata. PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0); // Get the attribution text. String attributions = photoMetadata.getAttributions(); // Create a FetchPhotoRequest. 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) { ApiException apiException = (ApiException) exception; int statusCode = apiException.getStatusCode(); // Handle error with given status code. Log.e(TAG, "Place not found: " + exception.getMessage()); } }); });
Attributions
In most cases, place photos can be used without attribution, or will have
the required attribution included as part of the image. However, if the
returned
PhotoMetadata
instance includes an attribution, you must
include the additional attribution in your application wherever you display the
image. For more information, see
Displaying Attributions.
Usage and billing
A Places Photo SKU is charged for calls to fetchPhoto()
.
See the Usage and
Billing page for details.