[英]Google Places API display places photo on recyclerview
我正在從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);
謝謝!
在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);
如果您需要更多幫助,請單擊此處,或者您可以發表評論,我會盡力為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.