Place Photos (New) lets you add high quality photographic content to your application. Place Photos gives you access to the millions of photos stored in the Places database. Place Photos returns a URI to a bitmap image. The bitmap image has a maximum size of 4800 by 4800 pixels.
Place Photos requests
To retrieve an image for a place:
- Use Place Details (New) to fetch a
Place
object usingfetchPlace()
. Be sure to include thePlace.Field PHOTO_METADATAS
field in the list of fields to include in the responsePlace
object. - In the
OnSuccessListener
for yourFetchPlaceResponse
, callPlace.getPhotoMetadas()
to get the photo metadata object of typePhotoMetadata
from the responsePlace
object. - Create a
FetchResolvedPhotoUriRequest
object to make the request and pass the photo metadata object, as well values for maximum height, maximum width, or both. - Use
PlacesClient.fetchResolvedPhotoUri()
to request the photo URI. - Add an
OnSuccessListener
and get the photo URI from theFetchResolvedPhotoUriResponse
object.
Required parameters
The required parameters for
FetchResolvedPhotoUriRequest
are:
-
Photo metadata
The metadata object of the photo to return.
-
Maximum height or maximum width
Specifies the maximum height and width, in pixels, of the image to return. If the image is smaller than the values specified, the original image will be returned. If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio. Both the maximum height and maximum width properties accept an integer between 1 and 4800. You must specify maximum height, maximum width, or both.
- To set the maximum height parameter, call the
setMaxHeight()
method when building theFetchResolvedPhotoUriRequest
object. - To set the maximum width parameter, call the
setMaxWidth()
method when building theFetchResolvedPhotoUriRequest
object.
- To set the maximum height parameter, call the
Place Photos examples
The following example demonstrates getting a place photo URI.
// 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 and author attributions. final String attributions = photoMetadata.getAttributions(); final AuthorAttributions authorAttributions = photoMetadata.getAuthorAttributions(); // Create a FetchResolvedPhotoUriRequest. final FetchResolvedPhotoUriRequest photoRequest = FetchResolvedPhotoUriRequest.builder(photoMetadata) .setMaxWidth(500) .setMaxHeight(300) .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. } }); });
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:
- Attributions, an attribution string accessed by
PhotoMetadata.getAttributions()
. - AuthorAttributions, an
AuthorAttributions
object accessed byPhotoMetadata.getAuthorAttributions()
.
If the returned PhotoMetadata
object includes either type of attribution, you must
include the attribution in your application wherever you display the image. For more information,
see Displaying Attributions.