Google Maps Cluster Item Marker Icon with Picasso

◇◆丶佛笑我妖孽 提交于 2019-12-11 09:03:41

问题


I'm using Google Map SDK 7.3.0 with android-maps-utils 0.3.4 because I need clusters for my Markers on the map.

Ok, so here the problem is, I shouldn't have a red marker. Only green+blue markers. I subclassed DefaultClusterRenderer to create my custom marker view but sometimes it just doesn't work.

I'm using picasso to get the green icon because it's coming from an API. But the problem is, when picasso has loaded the bitmap it's too late, the icon has already been set to the default one (red).

Here's my onBeforeClusterItemRenderer :

            Picasso.with(getApplicationContext()).load(item.url).into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                FrameLayout icon = (FrameLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker, null);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    icon.findViewById(R.id.bg).setBackground(new BitmapDrawable(getResources(), bitmap));
                } else {
                    icon.findViewById(R.id.bg).setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
                }


                Bitmap b = createDrawableFromView(Home.this, icon);

                if (marker != null) {
                    marker.icon(BitmapDescriptorFactory.fromBitmap(b));
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

回答1:


--- EDITED ---

When downloading the image inside onBeforeClusterItemRendered you are actually downloading the image every time the Cluster Manager tries to load a marker, so if you have, for example, 100 markers you will download the image 100 times.

You should download the image inside onCreate, save it in a static variable, call mClusterManager.cluster(); after saving the image, and finally inside onBeforeClusterItemRendered wrtie marker.icon(BitmapDescriptorFactory.fromBitmap(YourActivity.b));



来源:https://stackoverflow.com/questions/29947100/google-maps-cluster-item-marker-icon-with-picasso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!