Search code examples
javascriptasynchronousleaflet

Leaflet.js: detecting when map finishes zooming


So I'm making this application with leafet.js.

This application requires that I have to manually draw grids onto the screen, that I've taken care in a draw_grid() function that draws a bunch of polygons to the screen.

I have this function that I'm calling to trigger the change of the leaflet map.

zoom - the zoom integer and size is a dict like {x:1,y:1} that controls the size of the tiles drawn onto the map. (they need to change as the units the tiles are being drawn under are lat,long points on the map.

function changeZoom(zoom,size){
    map.setZoom(zoom); 
    setSize(size);   
    setTimeout(drawGrid,500)s;
}

The reason I have to use the setTimeout is because the leaflet ignores any drawing commands onto the map (which I'm doing as a layer) until the map has finished animating.

How to do this asynchronously instead?


Solution

  • You can use the map.zoomend event, described in the API here

    map.on('zoomend', function() {
        drawGrid();
    });
    

    Once the map finishes the zooming animation, it will then call the drawGrid function.