0

I am trying to add clusters in my leaflet map. I use overlayMaps which helps to choose one of the active layers, and in that case I want my marker to be a cluster. But it is not clustering. Again when I am trying with making any overlayMaps variable and put all the data its working the marker are cluster.

I want to show cluster in the map when the layer is active.

Plugin used: https://github.com/Leaflet/Leaflet.markercluster

var cluster = L.markerClusterGroup();
var geo = L.geoJSON(dataview, {
  onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.tags.name);
  },
});
geo.addTo(cluster);
var overlayMaps = {
  tourism: tourism,
  hospital: geo,
};
//add the layer control option
L.control.layers(baseMaps, overlayMaps).addTo(map);
1
  • 1
    Please edit your question and explain a bit more about "working fine" and "not working". – TomazicM Oct 7 '20 at 15:25
1

From what i can understand, your layer is not showing when you try to add it through the layer control.
Your code was almost right, you just needed to add cluster to overlayMaps instead of geo.
This code should work for you.

var cluster = L.markerClusterGroup();
var geo = L.geoJSON(dataview, {
  onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.tags.name);
  },
});
geo.addTo(cluster);
var overlayMaps = {
  tourism: tourism,
  hospital: cluster,
};
//add the layer control option
L.control.layers(baseMaps, overlayMaps).addTo(map);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.