在GoogleMap.InfoWindowAdapter毕加索图像加载问题 [英] Picasso image loading issue in GoogleMap.InfoWindowAdapter

查看:327
本文介绍了在GoogleMap.InfoWindowAdapter毕加索图像加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌地图API和希望的标记时,点击显示信息窗口内的图像。我实现了,但有一个关于毕加索的问题。当我点击一个标记,信息窗口显示和ImageView的显示图像占位符。如果我点击相同的标记一个更多的时间,成功地毕加索加载图像。我必须点击两次,它是如此的不可思议。

为了更好的理解游戏画面:

上的标记首先点击:

首先点击

当我再次点击:

再次点击

还有就是我的$ C $以下CS:

InfoWindowAdapter(毕加索加载这里)

  mMap.setInfoWindowAdapter(新GoogleMap.InfoWindowAdapter(){
        @覆盖
        公共查看getInfoWindow(标记标记){
            返回null;
        }        @覆盖
        公共查看getInfoContents(标记标记){
            查看myContentsView = getLayoutInflater()膨胀(R.layout.map_info_content,NULL);
            ImageView的ImageView的=(ImageView的)myContentsView.findViewById(R.id.imgView_map_info_content);
            Picasso.with(MainActivity.this)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(ImageView的);
            返回myContentsView;
        }
    });

(利用片段来传递图像URL)

设置标记

 为(MediaFeedData项目:mItemList){
        位置的LatLng =新经纬度(item.getLocation()。getLatitude()
                item.getLocation()getLongitude())。
        mMap.addMarker(新的MarkerOptions()
                是.snippet(item.getImages()。getLowResolution()。getImageUrl())
                .icon(BitmapDesc​​riptorFactory.fromResource(R.drawable.ic_point))
                .POSITION(位置));
    }

信息窗口布局XML

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>< ImageView的
    机器人:ID =@ + ID / imgView_map_info_content
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>< / LinearLayout中>


解决方案

我解决它。其实,这帖子就是答案。但是,我想解释如何做到这一点,并提供更多的细节,以解决我的情况这个问题。

在显示信息窗口,它被捕获的布局。这意味着,信息窗口不是动态的,它们只是一个图像,一个快照。这就是为什么,我们要刷新信息窗口后,图像加载成功。我们可以用毕加索的回调做到这一点。

我改变了我的codeS象下面这样:

我的新InfoWindowAdapter类:

 公共类MapInfoWindowAdapter实现GoogleMap.InfoWindowAdapter {
    私人最终哈希表<字符串,布尔值> markerSet;
    私人上下文的背景下;
    私人查看myContentsView;    公共MapInfoWindowAdapter(上下文的背景下,哈希表<字符串,布尔值> markerSet){
        this.context =背景;
        this.markerSet = markerSet;
    }    @覆盖
    公共查看getInfoWindow(标记标记){
        返回null;
    }    @覆盖
    公共查看getInfoContents(标记标记){
        LayoutInflater吹气=(LayoutInflater)上下文
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myContentsView = inflater.inflate(R.layout.map_info_content,NULL);
        ImageView的ImageView的=(ImageView的)myContentsView.findViewById(R.id.imgView_map_info_content);
        布尔isImageLoaded = markerSet.get(marker.getId());
        如果(isImageLoaded){
            Picasso.with(上下文)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(ImageView的);
        }其他{
            isImageLoaded = TRUE;
            markerSet.put(marker.getId(),isImageLoaded);
            Picasso.with(上下文)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(ImageView的,新的InfoWindowRefresher(标记));
        }        返回myContentsView;
    }
}

我传递给它的一个标志设置,它拥有的标记ID和一个布尔值,确定图像的标记加载与否。我检查这个选择哪个毕加索code应该运行。如果我不这样做,这将是递归。

InfoWindowRefresher:

 公共类InfoWindowRefresher实现回调{
    私人标记markerToRefresh;    公共InfoWindowRefresher(标记markerToRefresh){
        this.markerToRefresh = markerToRefresh;
    }    @覆盖
    公共无效的onSuccess(){
        markerToRefresh.showInfoWindow();
    }    @覆盖
    公共无效onerror的(){}
}

设置标志(用片段传递图像URL):

 的Hashtable<字符串,布尔值> markerSet =新的Hashtable<>();
对于(MediaFeedData项目:mItemList){
    位置的LatLng =新经纬度(item.getLocation()。getLatitude()
            item.getLocation()getLongitude())。
    的MarkerOptions的MarkerOptions =新的MarkerOptions()
            是.snippet(item.getImages()。getLowResolution()。getImageUrl())
            .icon(BitmapDesc​​riptorFactory.fromResource(R.drawable.ic_point))
            .POSITION(位置);
    标记标记= mMap.addMarker(的MarkerOptions);
    markerSet.put(marker.getId(),FALSE);
}mMap.setInfoWindowAdapter(新MapInfoWindowAdapter(这一点,markerSet));

I am using Google Maps Api and want to show an image inside InfoWindow when clicking of a Marker. I implemented it but there is a problem about Picasso. When I click a marker, infoWindow is shown up and imageView shows placeholder image. If I click same marker one more time, Picasso loads the image succesfully. I have to click twice, it is so weird.

Screenshots for better understanding:

First click on the marker:

When I click again:

There is my codes below:

InfoWindowAdapter (Picasso loading here)

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View myContentsView = getLayoutInflater().inflate(R.layout.map_info_content, null);
            ImageView imageView = (ImageView) myContentsView.findViewById(R.id.imgView_map_info_content);
            Picasso.with(MainActivity.this)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(imageView);
            return myContentsView;
        }
    });

Setting markers (using snippet to pass image url)

for (MediaFeedData item : mItemList) {
        LatLng position = new LatLng(item.getLocation().getLatitude(),
                item.getLocation().getLongitude());
        mMap.addMarker(new MarkerOptions()
                .snippet(item.getImages().getLowResolution().getImageUrl())
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_point))
                .position(position));
    }

InfoWindow layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
    android:id="@+id/imgView_map_info_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

解决方案

I solved it. Actually, this post is the answer. But I want to explain how to do it and give more details to solve this problem in my situation.

When showing an InfoWindow, it is captured of the layout. It means that InfoWindows are not dynamic, they are just a picture, a snapshot. That's why, we have to refresh the InfoWindow after image loaded successfully. We can use Picasso's Callback to do it.

I changed my codes like below:

My new InfoWindowAdapter Class:

public class MapInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
    private final Hashtable<String, Boolean> markerSet;
    private Context context;
    private View myContentsView;

    public MapInfoWindowAdapter(Context context, Hashtable<String, Boolean> markerSet) {
        this.context = context;
        this.markerSet = markerSet;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myContentsView = inflater.inflate(R.layout.map_info_content, null);
        ImageView imageView = (ImageView) myContentsView.findViewById(R.id.imgView_map_info_content);
        boolean isImageLoaded = markerSet.get(marker.getId());
        if (isImageLoaded) {
            Picasso.with(context)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(imageView);
        } else {
            isImageLoaded = true;
            markerSet.put(marker.getId(), isImageLoaded);
            Picasso.with(context)
                    .load(marker.getSnippet())
                    .placeholder(R.drawable.ic_placeholder)
                    .into(imageView, new InfoWindowRefresher(marker));
        }

        return myContentsView;
    }
}

I passed to it a markers set, it holds marker ids and a boolean to determine image for that marker is loaded or not. I am checking this to select which Picasso code should run. If I don't do this, it will be recursion.

InfoWindowRefresher:

public class InfoWindowRefresher implements Callback {
    private Marker markerToRefresh;

    public InfoWindowRefresher(Marker markerToRefresh) {
        this.markerToRefresh = markerToRefresh;
    }

    @Override
    public void onSuccess() {
        markerToRefresh.showInfoWindow();
    }

    @Override
    public void onError() {}
}

Setting markers (using snippet to pass image url):

Hashtable<String, Boolean> markerSet = new Hashtable<>();
for (MediaFeedData item : mItemList) {
    LatLng position = new LatLng(item.getLocation().getLatitude(),
            item.getLocation().getLongitude());
    MarkerOptions markerOptions = new MarkerOptions()
            .snippet(item.getImages().getLowResolution().getImageUrl())
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_point))
            .position(position);
    Marker marker = mMap.addMarker(markerOptions);
    markerSet.put(marker.getId(), false);
}

mMap.setInfoWindowAdapter(new MapInfoWindowAdapter(this, markerSet));

这篇关于在GoogleMap.InfoWindowAdapter毕加索图像加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆