Understand the method of marker clustering but unable to find the right solution for external geojson. I have tried various cluster plugins but nothing seems to work. See example map here.

var geojsonLayer = new L.GeoJSON.AJAX("https://raw.githubusercontent.com/marinespatialviz/marinespatialviz/master/litter.geojson", { pointToLayer: function(feature, yx) { var icon = L.icon({ iconUrl: feature.properties.gx_media_links, iconSize: [40, 40]}); var marker = L.marker(yx, {icon: icon}); marker.bindTooltip('<p>' + feature.properties.description + '</p>'); return marker; } }).addTo(map); var marker = L.markerClusterGroup(); marker.addLayer(geojsonLayer); map.addLayer(marker);

  • I looked at your code and haven't seen usage of clustering, can you please focus the question about specific lines? – NettaB Feb 21 '18 at 8:28
  • See edited code – Wanda Feb 25 '18 at 10:36

In order to use the geojson data for the clustering, you need to make sure that the data has loaded. The GeoJSON Ajax plugin has a listener for that - 'data: loaded': // Creating the markers var marker = L.markerClusterGroup(); geojsonLayer.on('data:loaded', function() { // Add the cluster data after the geojson layer has loaded. marker.addLayer(geojsonLayer); map.addLayer(marker); }) So I just transfered the markers initiation into this listener's function.

  • Hey @Wanda you're welcome. Please mark the answer as accepted, in the stack's way of thanking.... – NettaB Feb 25 '18 at 19:53

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.