Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the color of the clusters on my android Google map?

Currently I am trying to change the color of cluster icon (the default is blue), but I can't seem to figure it out. I have already set up my cluster manager and such, and I can see the icon itself, but it's blue. I am currently using the default Google set up (see code below) to set up my map. Any help would be appreciated

Thanks,

Jacob

public class MainActivity extends Activity {
    GoogleMap map;
    ClusterManager mClusterManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        map = mapFragment.getMap();
        setUpClusterer();
    }
    private void setUpClusterer() {
        // Declare a variable for the cluster manager.


        // Position the map.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

        // Initialize the manager with the context and the map.
        // (Activity extends context, so we can pass 'this' in the constructor.)
        mClusterManager = new ClusterManager<MyItem>(this, map);

        // Point the map's listeners at the listeners implemented by the cluster
        // manager.
        map.setOnCameraChangeListener(mClusterManager);
        map.setOnMarkerClickListener(mClusterManager);

        // Add cluster items (markers) to the cluster manager.
        addItems();
    }

    private void addItems() {

        // Set some lat/lng coordinates to start with.
        double lat = 51.5145160;
        double lng = -0.1270060;

        // Add ten cluster items in close proximity, for purposes of this example.
        for (int i = 0; i < 2; i++) {
            double offset = i / 60d;
            lat = lat + offset;
            lng = lng + offset;
            MyItem offsetItem = new MyItem(lat, lng);
            mClusterManager.addItem(offsetItem);
        }
    }
}


class MyItem implements ClusterItem {
    private final LatLng mPosition;

    public MyItem(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }
}
like image 477
Jacob Platin Avatar asked Apr 22 '15 01:04

Jacob Platin


1 Answers

As Verma noted in the comments it is possible to implement a fully customizable icon for your cluster. If all you want is to change the background color in the default icon, this code will do.

Either way you need to set a renderer for your ClusterManager:

mClusterManager.setRenderer(new CustomClusterRenderer(MainActivity.this, map, mClusterManager));

For CustomClusterRenderer you can extend DefaultClusterRenderer like so:

public class CustomClusterRenderer extends DefaultClusterRenderer<MyItem> {

    public CustomClusterRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) {
        super(context, map, clusterManager
    }

    @Override
    protected int getColor(int clusterSize) {
        return Color.BLUE // Return any color you want here. You can base it on clusterSize.
    }
}
like image 63
InnisBrendan Avatar answered Sep 17 '22 23:09

InnisBrendan