Skip to content

Instantly share code, notes, and snippets.

@b-g
Created September 9, 2015 11:48
Show Gist options
  • Save b-g/d8723c10be3ee598e259 to your computer and use it in GitHub Desktop.
Save b-g/d8723c10be3ee598e259 to your computer and use it in GitHub Desktop.
function long2tile(lon,zoom) {
return (((lon+180)/360*Math.pow(2,zoom)));
}
function lat2tile(lat,zoom) {
return (((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,zoom)));
}
function tile2long(x,z) {
return (x/Math.pow(2,z)*360-180);
}
function tile2lat(y,z) {
var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
@JaosnHsieh
Copy link

long2tile and lat2tile should be positive integer

 function long2tile(lon,zoom) { return (Math.floor((lon+180)/360*Math.pow(2,zoom))); }
 function lat2tile(lat,zoom)  { return (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))); }

from:
https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment