Created
June 19, 2020 19:02
-
-
Save pianosnake/0c9a20dd1d2bc4e534652bf43cf1b36e to your computer and use it in GitHub Desktop.
Convert meters to pixels when given a map zoom level and a latitude
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
const EARTH_CIR_METERS = 40075016.686; | |
function toRadians(degrees) { | |
return degrees * Math.PI / 180; | |
}; | |
const metersToPixels = (meters, latitude, zoom) => { | |
const metersPerPixel = EARTH_CIR_METERS / Math.pow(2, zoom + 8) * Math.cos(toRadians(latitude)); | |
return meters / metersPerPixel | |
} | |
// get a circle radius in pixels given the radius in meters, at zoom level 8 and latitude 40 | |
const pixelRadius = metersToPixels(1000, 40, 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment