Leaflet markercluster - on click zoom in then spiderfy

Viewed 1959

I am clustering on markers at the same location only (i.e. maxClusterRadius = 0). When clicking on the cluster I would like it to centre and zoom in at a specific zoom level (not max zoom), and then immediately spiderfy. Using the following code, the spiderfy does not occur after the zoom, but will if already at the desired zoom level. I suspect that this is because the cluster is considered to be different after zoom. How can I reference the new cluster (e.g. based on Lat/Long)?

  cluster.on('clusterclick', function (a) {

        if (map.getZoom() < 19) {
            map.once('zoomend', function() { a.layer.spiderfy(); });
            map.flyTo(a.layer.getLatLng(), 19); 
            }
        else
            a.layer.spiderfy();
        });    
1 Answers

Same problem. I have this workaround :

var mymap = L.map('mapid_<?= $this->id?>',{
    maxZoom:13,
    scrollWheelZoom:false,
}).setView([43.38388,-1.3049538], 7);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar', attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'}).addTo(mymap);

var markers = L.markerClusterGroup({
    disableClusteringAtZoom: 14,
    spiderfyOnMaxZoom: false,
    zoomToBoundsOnClick:false,
    animate:false
});

clickedMarker="";
mymap.on('zoomend', function() {
    if (clickedMarker!=="" && mymap.getZoom()>=mymap.options.maxZoom) {
        clickedMarker.__parent.spiderfy();
        clickedMarker="";
    }
});

markers.on('clusterclick', function (a) {
    if (a.layer._childCount>0) {
        clusterMarkers = a.layer.getAllChildMarkers();
        clickedMarker=clusterMarkers[0];
    }
    if (mymap.getZoom()>=mymap.options.maxZoom) {
        a.layer.spiderfy();
    } else {
        a.layer.zoomToBounds({padding: [20, 20]});
    }
});

Strange but spiderfy is not working with animate option set to true…

Related