TransWikia.com

Clustering markers from GeoJSON after adding data to layer in Leaflet

Geographic Information Systems Asked by Anagd Lamichhane on July 26, 2021

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

One Answer

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

Answered by Dror Bogin on July 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP