In a Leaflet map, when a school point is clicked it passes an id of that school and adds a student point layer specific to that school. The point layer is rendered on the map as marker cluster. So, clicking on another school will add student marker specific to that second school and so on. I want to display student marker cluster of each school with different colors.
Marker Cluster is defined as below:
var mcStudent= L.markerClusterGroup({
spiderfyOnMaxZoom: true,
disableClusteringAtZoom: 14,
maxClusterRadius: 5,
spiderfyDistanceMultiplier:10,
iconCreateFunction: function(cluster) {
var markers = cluster.getAllChildMarkers();
var html = markers.length;
$('<style>.stdCluster {background-color:'+bgcolor+';}</style>').appendTo('head'); //override class attribute (background color)
return new L.DivIcon({ html: html, className: 'stdCluster', iconSize: new L.Point(15, 15) });
}
});
The button calls a function loadStudent() which is defined as below:
var student;
var bgcolor;
function loadStudent(id){
bgcolor = '#'+(Math.random()*0xFFFFFF<<0).toString(16); //generate color code dynamically
$.ajax({
url:'php/findStudent.php',
type:'POST',
data:{id:id},
success: callbackStd
});
function callbackStd(response){
student = L.geoJSON(JSON.parse(response), {pointToLayer:function(feature,latlng){
return mcStudent.addLayer(L.marker(latlng,{icon: studentIcon}));
}});
student.addTo(mymap);
};
The issue is: marker cluster color changes dynamically as expected but it applies for all school student point clusters. For example, when a first school student point is loaded, the cluster icon color looks like below: When second school point is added. it changes the color for both first and second school: Now, when third school student point is added, it changes the color of all 3 schools. I want to display student marker cluster of each school with different color. I could not work out the logic here.