Forked from croxton/leaflet_custom_zoom_levels.js
Last active
March 28, 2017 10:03
-
-
Save jpdickerson/9939647 to your computer and use it in GitHub Desktop.
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
/* | |
Most map tile providers use 256px square tiles so Leaflet's basic zoom algorithm looks like this: | |
256 * Math.pow(2, zoom); | |
If you're working with vector layers you can extend one of leaflet's default CRSs | |
and make it return tile sizes in smaller increments. This can be very helpful when using | |
fitBounds() with layer groups, so the zoomed group fits better within the map bounds: | |
E.g., extending L.CRS.EPSG3857 (the default CRS): | |
*/ | |
L.CRS.CustomZoom = L.extend({}, L.CRS.EPSG3857, { | |
scale: function (zoom) { | |
return Math.round(256 * Math.pow(1.5, zoom)); // need to round since powers of 1.5 result in non-integers. Non-integers will not work with background tiles | |
} | |
}); | |
var map = new L.Map('map', { | |
crs: L.CRS.CustomZoom | |
}); | |
/* | |
It's worth noting you can also swap the CRS on the fly if you need to swap between a vector layer and a tile layer | |
*/ | |
// swap back to standard zoom | |
map.options.crs = L.CRS.EPSG3857; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work in leaflet 0.7.x? I found the tiles don't display with the above settings.