Google Places API显示将照片放置在recyclerview上

时间:2018-08-17 01:15:08

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

我正在从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);

谢谢!

1 个答案:

答案 0 :(得分:3)

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或发表评论,我会尽力帮助您。