Skip to content

Instantly share code, notes, and snippets.

@pianosnake
Created June 19, 2020 19:02
Show Gist options
  • Save pianosnake/0c9a20dd1d2bc4e534652bf43cf1b36e to your computer and use it in GitHub Desktop.
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
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