Skip to content

Instantly share code, notes, and snippets.

@kikuchan
Created November 19, 2013 19:16
Show Gist options
  • Save kikuchan/7550865 to your computer and use it in GitHub Desktop.
Save kikuchan/7550865 to your computer and use it in GitHub Desktop.
Obey maxBounds during dragging a map on leaflet 0.7
/*
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