Created
June 26, 2014 11:03
-
-
Save Zverik/09961fbd60257bdf26c9 to your computer and use it in GitHub Desktop.
Leaflet LimitZoom plugin
This file contains hidden or 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
// Limit leaflet map zoom to a list of variants | |
// Written by Ilya Zverev, licensed WTFPL | |
L.Map.mergeOptions({ | |
zooms: [] // array of integers | |
}); | |
L.Map.include({ | |
_limitZoom: function (zoom) { | |
var zooms = this.options.zooms; | |
if (!zooms || !('length' in zooms) || !zooms.length) { | |
var min = this.getMinZoom(), | |
max = this.getMaxZoom(); | |
return Math.max(min, Math.min(max, zoom)); | |
} else { | |
var z, d = 100, i, dist; | |
var dz = -1, dd = 100, dir = zoom - this._zoom; | |
for (i = 0; i < zooms.length; i++) { | |
dist = Math.abs(zooms[i] - zoom); | |
if (dist < d) { | |
z = zooms[i]; | |
d = dist; | |
} | |
if (dir * (zooms[i] - this._zoom) > 0 && dist < dd) { | |
dz = zooms[i]; | |
dd = dist; | |
} | |
} | |
return dz < 0 ? z : dz; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: this plugin was moved to a proper GitHub repository: https://github.com/Zverik/Leaflet.LimitZoom