【问题标题】:Android: How to display photos from Google Places APIAndroid:如何显示来自 Google Places API 的照片
【发布时间】:2013-02-13 21:35:35
【问题描述】:

我有一个应用程序从 Google Places API 返回一个地点列表。当用户单击地图上的标记(代表地点)时,会弹出一个对话框,其中包含有关该地点的详细信息。我还想展示该地点的照片(如果有的话)。

我从这里知道 Place Photos API:https://developers.google.com/places/documentation/photos#place_photo_response

我能够从 Place Details 响应中检索 photo_reference,然后我可以将它与传感器、maxwidth/maxheight 和我的 API 密钥一起传递给 Place Photos API 请求。我遇到的问题是从响应中获取照片。如何将照片对象检索为位图,以便在对话框中显示它。

【问题讨论】:

    标签: android google-places


    【解决方案1】:

    经过几个小时的挫折,我找到了解决方案。

    通过在 API 返回的响应上调用 getContentType(),我能够确定响应只是给定类型的图像。就我而言,我发现它是“text/jpeg”。您可以通过调用getContent()来检索内容的InputStream

    BitmapFactory 类允许您将InputStream 解码为Bitmap

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

    为了显示这个图像,我首先需要为DialogBuilder 设置一个自定义视图。

    LayoutInflater factory = LayoutInflater.from(mapView.getContext());
    final View view = factory.inflate(R.layout.dialog_layout, null);
    dialog.setView(view);
    

    接下来,我获取照片响应并将其添加到布局中的ImageView(已在 XML 中定义):

    LinearLayout ll = (LinearLayout) view.findViewById(R.id.dialogLayout);
    ImageView imageView = (ImageView) ll.findViewById(R.id.placePhoto);
    imageView.setImageBitmap(photo);
    

    请务必注意,“地点详情”响应最多可以发回 10 张照片。您不能保证“最佳”照片会被退回。就我而言,我选择了第一个。

    【讨论】:

      【解决方案2】:

      最新版本的 Google Play 服务 (7.8) 包括获取与地点相关联的照片的功能 - 有关 javadoc,请参阅 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-24
        • 1970-01-01
        • 2016-12-25
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多