1

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

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

1 Answer 1

1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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