1
votes

I use angularjs and leaflet.js.

In my controller I set a listener on the leaflet map zoom :

map.on("zoomend", function () {
    refreshData(...); // use the zoom & bound to get data.
});

The refreshData() refresh the markers displayed in the map according to many criteria ( facets ) and re adjust the map with setView(). ( refreshData() can also be called by other function )

var refreshData = function(){
    // lot of thinks...
    map.setView(new L.LatLng(lat,lon),zoomlevel);
}

So when refreshData is called i want to disable the map.on("zoomend") listener because it make a cyclic call.

How to do this ?

2

2 Answers

2
votes

I would use a flag variable.

map.on("zoomend", refreshData);

var refreshData = function(){
    map.off("zoomend");
    // lot of thinks...
    map.setView(new L.LatLng(lat,lon),zoomlevel);
    map.on("zoomend", refreshData);
}
0
votes

Thanks to @pk. for the answer. I had to add a timeout to make it work properly because the map.on("zoomend", refreshData); is called just after the call of map.setView(new L.LatLng(lat,lon),zoomlevel); who take some times to finish :

var refreshData = function(){
    map.off("zoomend");
    // lot of thinks...
    map.setView(new L.LatLng(lat,lon),zoomlevel);
    $timeout(function () {
        map.on("zoomend", refreshData);
    },3000);
}

the best way will be with a callback on setView but I don't find a way to do something like this :

var refreshData = function(){
    map.off("zoomend");
    // lot of thinks...
    map.setView(
        new L.LatLng(latlon["lat"], latlon["lon"]),
        GeographieService.getZoomFromLocalisationtype("continents"), 
        {'reset' : true }
    ).then(function (data) {
        map.on("zoomend", refreshData);
    });
}

any idea ?