Last active
December 12, 2015 10:09
-
-
Save ismyrnow/4757196 to your computer and use it in GitHub Desktop.
Get WebMercator zoom level given a radius in miles
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
function getZoomFromRadius(radius) { | |
// Calculates the target zoom level given a radius in miles | |
var mapDiv = $("#map"); | |
var mapWidth = mapDiv.width(); | |
var mapHeight = mapDiv.height(); | |
var radiusMeters = radius * 1609.34; // converting miles to meters | |
var diameter = radiusMeters * 2; | |
var basePixelSize = 156543.03392799936; // pixel size in meters at zoom level 0 | |
var pixelSize = Math.max((diameter / mapWidth), (diameter / mapHeight)); | |
var zoomFactor = 2; // default for tiled basemap | |
var zoom = Math.log(basePixelSize / pixelSize) / Math.log(zoomFactor); | |
return Math.round(zoom); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment