Android的:如何显示来自谷歌API的地方的照片照片、地方、Android、API

2023-09-06 23:22:16 作者:こ兮忆

我有一个正在返回从谷歌API的地方一个地方列表中的应用程序。当用户点击一个标记(RE presenting的地方)在地图上,将弹出一个对话框了那个地方的细节。我想也显示了地方的照片(如果有的话)。

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

我知道的地方的照片API从这里:的https://developers.google.com/places/documentation/photos#place_photo_response

I am aware of the Place Photos API from here : https://developers.google.com/places/documentation/photos#place_photo_response

我能够检索来自地方详情响应photo_reference,我可以传递到地方的照片API请求,以及传感器,了maxWidth /和了maxHeight我的API密钥。我遇到的问题是获得来自响应的照片。如何取回照片对象作为位图,这样我可以在对话框中显示出来。

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.

推荐答案

我发现后,一对夫妇的无奈小时的解决方案。

I have found a solution after a couple of hours of frustration.

通过对响应从API调用回的getContentType(),我能确定的响应是一个简单的给定类型的图像。就我而言,我发现它是text / JPEG。你可以通过调用检索的InputStream 内容的的getContent()

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 类,您可以去$ C C的的InputStream $成位图

BitmapFactory class allows you to decode an InputStream into a Bitmap.

Bitmap photo = BitmapFactory.decodeStream(request.execute().getContent());

为了显示这个图片,我首先需要设置为 DialogBu​​ilder 自定义视图。

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

接下来,我得到的光响应,并把它添加到的ImageView 在布局(它已经在XML中定义):

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

要注意的是,地方详情响应可以发回多达10张照片是很重要的。你不能保证最好的照片将被送回。就我而言,我拿的第一个。

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.