Skip to content

Instantly share code, notes, and snippets.

@mlevans
Created April 27, 2012 05:08
Show Gist options
  • Save mlevans/2506045 to your computer and use it in GitHub Desktop.
Save mlevans/2506045 to your computer and use it in GitHub Desktop.
Convert Latitude/Longitude Pair to Tile Coordinates
function latLngToCoords(zoom,lat,lon) {
var n = Math.pow(2,zoom);
var lat_rad = lat * (Math.PI / 180);
var x_coord = n * ((lon + 180) / 360);
var y_coord = .5 * n * (1 - (Math.log (Math.tan(lat_rad) + (1/Math.cos(lat_rad))) / Math.PI));
return {x: Math.floor(x_coord), y: Math.floor(y_coord)};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment