【问题标题】:Google Places API display places photo on recyclerviewGoogle Places API 在 recyclerview 上显示位置照片
【发布时间】:2019-01-24 00:18:07
【问题描述】:

我正在从 google Places API 获取地点数据,它一次返回 20 个结果。我正在将数据显示到 recyclerview 中。那么在不超过 google api 的每日配额的情况下,在 recyclerview 中显示所有 20 个地点图像的最佳方法是什么。

我已经尝试过了,但它不起作用,我无法弄清楚它为什么不起作用。

String url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="
                    + places.getPhotoRefrence() + "&sensor=true&key=" + key;

Glide.with(context)
                    .load(url)
                    .into(holder.placeListIV);

谢谢!

【问题讨论】:

    标签: android google-maps google-maps-api-3 google-places-api android-glide


    【解决方案1】:

    onBindViewHolder() 方法下面的适配器类上实现此

    /**
    * Fetching photos of places using placeId and setting photos on imageview of recyclerview
         */
        if (!places.getPhotoRefrence().equals("null")) {
            String photorefrence = places.getPhotoRefrence();
            String url = baseUrl + photorefrence + "&key=" + key;
    
    
            final String placeId = places.getPlaceId();
            final Task<PlacePhotoMetadataResponse> photoMetadataResponse = mGeoDataClient.getPlacePhotos(placeId);
            photoMetadataResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoMetadataResponse>() {
                @Override
                public void onComplete(@NonNull Task<PlacePhotoMetadataResponse> task) {
                    // Get the list of photos.
                    PlacePhotoMetadataResponse photos = task.getResult();
                    // Get the PlacePhotoMetadataBuffer (metadata for all of the photos).
                    PlacePhotoMetadataBuffer photoMetadataBuffer = photos.getPhotoMetadata();
                    // Get the first photo in the list.
                    PlacePhotoMetadata photoMetadata = photoMetadataBuffer.get(0);
                    // Get the attribution text.
                    CharSequence attribution = photoMetadata.getAttributions();
                    // Get a full-size bitmap for the photo.
                    Task<PlacePhotoResponse> photoResponse = mGeoDataClient.getPhoto(photoMetadata);
                    photoResponse.addOnCompleteListener(new OnCompleteListener<PlacePhotoResponse>() {
                        @Override
                        public void onComplete(@NonNull Task<PlacePhotoResponse> task) {
                            PlacePhotoResponse photo = task.getResult();
                            Bitmap bitmap = photo.getBitmap();
                            holder.placeListIV.setImageBitmap(bitmap);
                        }
                    });
                }
            });
        }
    

    别忘了初始化mGeoDataClient

    mGeoDataClient = com.google.android.gms.location.places.Places.getGeoDataClient(this, null);

    如果您需要更多帮助,请点击here 或者您可以发表评论,我会尽力帮助您。

    【讨论】:

    • 哇,这真是太棒了。感谢您提供帮助和分享此代码。
    • 随时提问 Stackoverflow 是一个很好的社区,可以帮助像我们这样的开发人员。很高兴它对你有用。
    • 我们应该把下载的图片缓存起来,避免每次都下载
    • 如果我知道我会告诉他,那么请分享一下如何缓存从 google Places API 下载的图片并使用缓存中的图片。
    • 这对我很有用。我通过使用 AsyncTask 来过度思考解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    相关资源
    最近更新 更多