Skip to content

Instantly share code, notes, and snippets.

@ksnabb
Created August 26, 2013 18:56
Show Gist options
  • Save ksnabb/6345157 to your computer and use it in GitHub Desktop.
Save ksnabb/6345157 to your computer and use it in GitHub Desktop.
CoffeeSript version of counting tile x y z or coordinates from tile x y z.
long2tile = (lon, zoom) ->
Math.floor (lon + 180) / 360 * Math.pow(2, zoom)
lat2tile = (lat, zoom) ->
Math.floor (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom)
tile2long = (x, z) ->
x / Math.pow(2, z) * 360 - 180
tile2lat = (y, z) ->
n = Math.PI - 2 * Math.PI * y / Math.pow(2, z)
180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment