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?
-
1What library are you using to create map?– ArashMadCommented Sep 4, 2020 at 20:08
-
I'm using JavaScript– user169554Commented Sep 4, 2020 at 22:11
-
I meant name of the library. There are different libraries for creating an interactive map.– ArashMadCommented Sep 5, 2020 at 4:51
-
My apologies, I'm relatively new to programming. I'm using Carto VL to build my map– user169554Commented Sep 7, 2020 at 15:34
Add a comment
|
1 Answer
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
});