detect if leaflet map is zoomed in or zoomed out? - Javascript Leaflet

Javascript examples for Leaflet:Zoom

Description

detect if leaflet map is zoomed in or zoomed out?

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Leaflet demo</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script> 
      <link rel="stylesheet" type="text/css" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css"> 
      <style id="compiled-css" type="text/css">

#map {/*from   w  w w. j a  v  a 2  s  .co  m*/
   height: 500px;
   width: 80%;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
       var osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
           osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
           osm = L.tileLayer(osmUrl, {
               maxZoom: 18,
               attribution: osmAttrib
           });
       var map = L.map('map').setView([0,0], 2).addLayer(osm);
      var prevZoom = map.getZoom();
map.on('zoomend',function(e){
   debugger;
   var currZoom = map.getZoom();
  var diff = prevZoom - currZoom;
  if(diff > 0){
     console.log('zoomed out');
  } else if(diff < 0) {
     console.log('zoomed in');
  } else {
     console.log('no change');
  }
  prevZoom = currZoom;
});
    }

      </script> 
   </head> 
   <body> 
      <div id="map"></div>  
   </body>
</html>

Related Tutorials