Created
November 13, 2013 18:22
-
-
Save kikuchan/7453817 to your computer and use it in GitHub Desktop.
Try to restrict user to drag to outside of maxBounds on Leaflet 0.6.4, nicely. (It access private objects, so it might not work properly on another version)
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
L.Map.addInitHook(function() { | |
var map = this; | |
this.dragging._draggable.on("predrag", function() { | |
var bounds_sw = map.latLngToLayerPoint(map.options.maxBounds.getSouthWest()).add(this._newPos); | |
var bounds_ne = map.latLngToLayerPoint(map.options.maxBounds.getNorthEast()).add(this._newPos); | |
var size = map.getSize(); | |
if (bounds_ne.y > 0) { | |
this._newPos.y -= bounds_ne.y / 1.2; | |
} | |
if (bounds_sw.x > 0) { | |
this._newPos.x -= bounds_sw.x / 1.2; | |
} | |
if (bounds_ne.x < size.x) { | |
this._newPos.x -= (bounds_ne.x - size.x) / 1.2; | |
} | |
if (bounds_sw.y < size.y) { | |
this._newPos.y -= (bounds_sw.y - size.y) / 1.2; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment