Search code examples
androidgoogle-mapsandroid-maps-utils

Google Maps Cluster Item Marker Icon with Picasso


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.

My 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) {

            }
        });

Solution

  • --- 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));