map.getZoom() not working?

4,082 views
Skip to first unread message

Thomas Möller

unread,
Mar 18, 2014, 4:39:28 AM3/18/14
to leafl...@googlegroups.com
I am trying to get the current zoom level but it doesn´t seem to work. What am I doing wrong?

For debugging purposes I tried a simple output of the zoom level in a DIV but it just shows the zoom level when loading
the page. After zooming the tile layer gets invisible and the DIV shows the same level as before.


 function onZoomend() {
    var currentZoom = map.getZoom();
    document.getElementById("zlevel").innerHTML=currentZoom;
 };

map.on('zoomend', onZoomend());

Charlie Geiger

unread,
Mar 18, 2014, 3:14:52 PM3/18/14
to leafl...@googlegroups.com
For starters, map.on('zoomend', onZoomend()); should be: map.on('zoomend', onZoomend);

If that does not fix it, post back.

Thomas Möller

unread,
Mar 19, 2014, 3:59:55 AM3/19/14
to leafl...@googlegroups.com
Hmm I don´t understand that but it works. Thanks.

Pat Keller

unread,
Mar 19, 2014, 9:21:46 AM3/19/14
to leafl...@googlegroups.com
I wanted to know why as well and found this in the Leaflet documentation, "Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:"

Charlie Geiger

unread,
Mar 19, 2014, 3:14:55 PM3/19/14
to leafl...@googlegroups.com
It works because it is correct JavaScript syntax. You cannot pass a function as a parameter with parameters in the syntax that you used. If you need to pass a function as a parameter with parameters you need to wrap it inside of another function, or use the 'evil' eval method.

Both of these will work equally:
map.on('zoomend', onZoomend);
or
map.on('zoomend', function() { onZoomend(); });

Look up passing a function as an argument/parameter to read up on it further.
Reply all
Reply to author
Forward
0 new messages