Created
August 22, 2014 21:20
-
-
Save spalladino/231bbec139d59b70be45 to your computer and use it in GitHub Desktop.
Handler for leaflet.js to manage max bounds without bouncing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize OSM source, based on map.html example | |
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', | |
osmAttrib = '© <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