Reputation: 24028
I have an application which is returning a list of places from the Google Places API. When a user clicks on a marker (representing the place) on a map, a Dialog box pops up with details about that place. I would like to also display a photo of the place (if one exists).
I am aware of the Place Photos API from here : https://developers.google.com/places/documentation/photos#place_photo_response
I am able to retrieve the photo_reference from the Place Details response, and I can pass that to the Place Photos API request, along with sensor, maxwidth/maxheight and my API key. The problem I am having is getting the photo from the response. How do I retrieve the photo object as a Bitmap so that I can display it in the Dialog box.
Upvotes: 3
Views: 4974
Reputation: 191
The latest release of Google Play Services (7.8) includes the ability to get the photos associated with a Place - see here for the javadoc.
Upvotes: 1
Reputation: 24028
I have found a solution after a couple of hours of frustration.
By calling getContentType(
) on the response back from the API, I was able to identify that the response is simply an image of a given type. In my case I found it to be "text/jpeg". You can retrieve the InputStream
of the content by calling getContent()
.
BitmapFactory
class allows you to decode an InputStream
into a Bitmap
.
Bitmap photo = BitmapFactory.decodeStream(request.execute().getContent());
In order to display this image, I first needed to set a custom view for the DialogBuilder
.
LayoutInflater factory = LayoutInflater.from(mapView.getContext());
final View view = factory.inflate(R.layout.dialog_layout, null);
dialog.setView(view);
Next, I get the photo response and add it to the ImageView
in the layout (which is already defined in the XML):
LinearLayout ll = (LinearLayout) view.findViewById(R.id.dialogLayout);
ImageView imageView = (ImageView) ll.findViewById(R.id.placePhoto);
imageView.setImageBitmap(photo);
It is important to note that the Place Details response can send back up to 10 photos. You can't guarantee that the "best" photo will be sent back. In my case, I take the first one.
Upvotes: 7