Created
November 19, 2013 19:16
-
-
Save kikuchan/7550865 to your computer and use it in GitHub Desktop.
Obey maxBounds during dragging a map on leaflet 0.7
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
/* | |
Obey maxBounds during dragging a map on leaflet 0.7 | |
New option "maxBoundsResistance" (0.0 - 1.0) controls its reaction; | |
0.0: No effect | |
0.8: Modern reaction (default) | |
1.0: Strictly forbid | |
*/ | |
L.Map.addInitHook(function() { | |
var map = this; | |
this.dragging._draggable.on("predrag", function() { | |
var viewHalf = map.getSize().divideBy(2); | |
var centerPoint = map.getPixelOrigin().add(viewHalf).subtract(this._newPos); | |
var viewBounds = new L.Bounds(centerPoint.subtract(viewHalf), centerPoint.add(viewHalf)); | |
var offset = map._getBoundsOffset(viewBounds, map.options.maxBounds, map.getZoom()); | |
this._newPos = this._newPos.subtract(offset.multiplyBy(map.options.maxBoundsResistance || 0.8)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment