Stop panning Leaflet map out of the world's edge? - Javascript Leaflet

Javascript examples for Leaflet:Map

Description

Stop panning Leaflet map out of the world's edge?

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script> 
      <style id="compiled-css" type="text/css">

html, body, #map {
   width: 100%;
   height: 100%;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){/*w ww. j av  a  2s.c o  m*/
var map = L.map('map').setView([51.505, -0.09], 3);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
var southWest = L.latLng(-89.98155760646617, -180),
northEast = L.latLng(89.99346179538875, 180);
var bounds = L.latLngBounds(southWest, northEast);
map.setMaxBounds(bounds);
map.on('drag', function() {
   map.panInsideBounds(bounds, { animate: false });
});
    }

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

Related Tutorials