Skip to content

Instantly share code, notes, and snippets.

@spalladino
Created August 22, 2014 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spalladino/231bbec139d59b70be45 to your computer and use it in GitHub Desktop.
Save spalladino/231bbec139d59b70be45 to your computer and use it in GitHub Desktop.
Handler for leaflet.js to manage max bounds without bouncing
// Initialize OSM source, based on map.html example
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib});
// Create map with max bounds
var map = L.map('map')
.setView([50.5, 30.51], 15)
.addLayer(osm)
.setMaxBounds([[50.52979753992208, 30.527229309082035],[50.497049833624224, 30.458564758300785]]);
// Attach custom handler to predrag event of draggable component of map
map.dragging._draggable.on('predrag', function() {
var currentTopLeft = map._initialTopLeftPoint.subtract(this._newPos);
var currentBounds = new L.Bounds(currentTopLeft, currentTopLeft.add(map.getSize()));
var limitedOffset = map._getBoundsOffset(currentBounds, map.options.maxBounds);
this._newPos = this._newPos.subtract(limitedOffset);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment