Skip to content

Instantly share code, notes, and snippets.

@kikuchan
Created November 13, 2013 18:22
Show Gist options
  • Save kikuchan/7453817 to your computer and use it in GitHub Desktop.
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)
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