Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom icon of cluster group in android

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));
like image 461
tej shah Avatar asked Apr 24 '17 05:04

tej shah


3 Answers

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

}
like image 81
Aryan Najafi Avatar answered Nov 03 '22 06:11

Aryan Najafi


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)
         }
like image 23
Ринат Avatar answered Nov 03 '22 06:11

Ринат


    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");
        }
    }
like image 29
tej shah Avatar answered Nov 03 '22 04:11

tej shah