Google 地点 API 在 recyclerview 上显示地点照片

Google Places API display places photo on recyclerview

我正在从 google 个地方 API 获取地点数据,它一次 returns 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);

谢谢!

在你的适配器 class 下面 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,或者您可以发表评论,我会尽力帮助您。