Search code examples
javagoogle-mapsandroid-studioiconsmarkerclusterer

How do I change the icons for the cluster objects in java google maps


I'm stuck with a little problem in the Clustering library for google maps in Android studio. The problem is that I only have the default icons for the cluster objects, like these icons.

I would like to give the markers a custom icon, but I cant find anything about it on internet. And I find it too unclear on the library page.

Thanks in advance!


Solution

  • I found a solution! Here it is:

    @Override
    protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions)
    {
    
        final Drawable clusterIcon = ContextCompat.getDrawable(MainActivity.context,R.drawable.[your_icon]);
    
        mClusterIconGenerator.setBackground(clusterIcon);
    
        //modify padding for one or two digit numbers
        if (cluster.getSize() < 10) {
            mClusterIconGenerator.setContentPadding(40, 20, 0, 0);
        }
        else 
        {
            mClusterIconGenerator.setContentPadding(30, 20, 0, 0);
        }
    
        Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
    
    }
    

    This worked for me.