I'm building a map that uses a conditional statement to determine what data to load based on the zoom level. However, the conditional statement is only fired when the map first loads, as opposed to every time the zoom level changes. Is there a way to change the code to run on zoom load (i.e. to update every time the user zooms in or out) rather than the initial page load?

  • 1
    What library are you using to create map?
    – ArashMad
    Sep 4, 2020 at 20:08
  • I'm using JavaScript
    – user169554
    Sep 4, 2020 at 22:11
  • I meant name of the library. There are different libraries for creating an interactive map.
    – ArashMad
    Sep 5, 2020 at 4:51
  • My apologies, I'm relatively new to programming. I'm using Carto VL to build my map
    – user169554
    Sep 7, 2020 at 15:34

1 Answer 1


All map libraries have events which help you to find out when map zoomed, clicked and panned. For example, if you're using Leaflet.js, map object has following listener for zoom:

var initCenter = [42.444508, -76.499491];
var initZoom = 4;

var map = L.map('map').setView(initCenter, initZoom);

map.on('zoomend', function() {
    currentZoomLevel = map.getZoom();
    // Do your job

On the other hand, you can find map events for CARTO here.

map.on('move', {
    currentZoomLevel = map.getZoom();
    // Do your job

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.