Place Photos

Select platform: Android iOS JavaScript Web Service

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.

Places SDK for Android returns a bitmap image with a maximum size of 1600 by 1600 pixels.

Photo retrieval process

To retrieve an image for a place:

  1. UsePlace Detailsto fetch aPlaceobject (use either fetchPlace()or findCurrentPlace()). Be sure to include thePlace.Field PHOTO_METADATASfield in the list of fields to include in the responsePlaceobject.
  2. In the OnSuccessListener for your FetchPlaceResponse orFindCurrentPlaceResponse, usePlace.getPhotoMetadas()to get the photo metadata object, of type PhotoMetadata from the responsePlaceobject.
  3. Create aFetchPhotoRequestobject, optionally specifying maximum height and width (in pixels). Photos can have a maximum width or height of 1600px.
  4. UsePlacesClient.fetchPhoto() to request the photo bitmap.
  5. Add anOnSuccessListenerand get the photo from the FetchPhotoResponse.

Get a photo

The following example demonstrates getting a place photo:

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

Attributions

In most cases, place photos can be used without attribution, or will have the required attribution included as part of the image. However, the photo metadata object, of type PhotoMetadata, can contain either of two types of additional attributions:

If the returnedPhotoMetadataobject includes either type of attribution, you must include the attribution in your application wherever you display the image. For more information, seeDisplaying Attributions.

Usage and billing

APlaces PhotoSKU is charged for calls tofetchPhoto(). See theUsage and Billingpage for details.