I'm trying to find a way to calculate zoom level given a radius in meters.
For example, I want to show 100km radius
within point x,y
, how do I calculate the proper zoom level for that point?
This will depend on:
The resolution (meters/pixel) of a web-Mercator map at zoom 0 (one tile for the whole map) is given by:
resolution = cos(latitude_rad) * earth circumference / tile width
with the latitude in radians, the earth circumference in meters (2 * pi * radius) and the map width given by:
map width = 256
as the tiles are 256 pixel wide.
At each zoom step, the width (in pixel) is doubled, so at zoom z:
map width = 256*2^zoom
so :
resolution=cos(latitude_rad) * 2*pi* 6371008 / (256*2^zoom)
but as you are using a certain number n of 256 pixel tiles:
widthmap = cos(latitude_rad) * 2*pi* 6371008*n / (2^zoom)
Now, to get the zoom from the distance:
with latitude in degrees, radius is your circle in meters, and n the number of tiles to display:
zoom = floor(log2 ((cos(latitude *pi/180) * 2*pi* 6371008*n)/radius))
(floor as you want an integer and to make sure your whole circle is on the map)
For example, at 45 degrees N/S, 100m and 1 tile will give you a zoom of 18.