custom icon of cluster group in android
Asked Answered
F

3

8

I want to change defult cluster group icon with my drawable icon. Please check my code for cluster program.

MyClusterRenderer.java

    public  class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

          public MyClusterRenderer(Context context, GoogleMap map,
                                   ClusterManager<MyItem> clusterManager) {
              super(context, map, clusterManager);

          }

          @Override
          protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
              super.onBeforeClusterItemRendered(item, markerOptions);

 markerOptions.title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));// for marker
      }

          @Override
          protected void onClusterItemRendered(final MyItem clusterItem, Marker marker) {
              super.onClusterItemRendered(clusterItem, marker);

          }


      }

code in my fragment for setup cluser

  googleMap.clear();
        mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap);

        googleMap.setOnMarkerClickListener(mClusterManager);
        googleMap.setOnCameraChangeListener(mClusterManager);
        readItems();
        mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), googleMap, mClusterManager));
Francenefrances answered 24/4, 2017 at 5:36 Comment(0)
A
4

Try to change Cluster icon in onBeforeClusterRendered method instead of onBeforeClusterItemRendered :

@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions) {

    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));

}
Approbation answered 24/4, 2017 at 11:0 Comment(1)
This changes the item icon, not the cluster icon.Adoree
A
7

The best way is

         private val clusterIconGenerator = IconGenerator(context)

         override fun getDescriptorForCluster(cluster: Cluster<PromotionMarker>): BitmapDescriptor {
            clusterIconGenerator.setBackground(ContextCompat.getDrawable(context, R.drawable.background_marker))
            val icon: Bitmap = clusterIconGenerator.makeIcon(cluster.size.toString())
            return BitmapDescriptorFactory.fromBitmap(icon)
         }
Ait answered 3/10, 2020 at 18:16 Comment(2)
Thanks so much for this answer! onBeforeClusterRendered was called in my code, but was not changing the icon (which is rather odd, since this does work in two other ClusterRenderers in this project). Using getDescriptorForCluster instead made it work perfectly!Girard
This works good, but my cluster count string appears outside of the bitmap instead of in it. Any ideas?Adoree
A
4

Try to change Cluster icon in onBeforeClusterRendered method instead of onBeforeClusterItemRendered :

@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions) {

    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));

}
Approbation answered 24/4, 2017 at 11:0 Comment(1)
This changes the item icon, not the cluster icon.Adoree
F
4
    public  class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {
        private final IconGenerator mClusterIconGenerator = new IconGenerator(
                getActivity());
        public MyClusterRenderer(Context context, GoogleMap map,
                                 ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);


            View multiProfile = getActivity().getLayoutInflater().inflate(
                    R.layout.cluster_custome_icon, null);
            mClusterIconGenerator.setContentView(multiProfile);
        }

        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {

         markerOptions.title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_both));

            super.onBeforeClusterItemRendered(item, markerOptions);
        }

//        @Override
//        protected void onClusterItemRendered(final MyItem clusterItem, Marker marker) {
//            super.onClusterItemRendered(clusterItem, marker);
//
//        }
        @Override
        protected void onBeforeClusterRendered(Cluster<MyItem> cluster,
                                               MarkerOptions markerOptions) {

            Log.e("get_item_list_nir", "CallMap onBeforeClusterRendered 13");
            try {
                mClusterIconGenerator.setBackground(null);
                Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster
                        .getSize()));
                markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("get_item_list_nir", "error 13.1 : " + e.toString());
            }
            Log.e("get_item_list_nir", "CallMap onBeforeClusterRendered 14");
        }
    }
Francenefrances answered 24/4, 2017 at 12:15 Comment(2)
Great! Using your code, it's easy to generate any icons for clusters. Thanks!Subinfeudation
How do you set a custom background drawable with the count string?Adoree

© 2022 - 2024 — McMap. All rights reserved.