Skip to content

Instantly share code, notes, and snippets.

@mattfawcett
Created March 2, 2011 14:10
Show Gist options
  • Save mattfawcett/850986 to your computer and use it in GitHub Desktop.
Save mattfawcett/850986 to your computer and use it in GitHub Desktop.
For a given latitude, longitude and zoom level return the Google maps tile reference for that location - as documented at http://code.google.com/apis/maps/documentation/javascript/v2/overlays.html#Projections
def tile_info(lat, lon, zoom)
rad_lat = lat / 180 * Math::PI
rad_lon = lon / 180 * Math::PI
merc_x = rad_lon
merc_y = Math.log( Math.tan(rad_lat) + 1/ Math.cos(rad_lat) )
cart_x = merc_x + Math::PI
cart_y = Math::PI - merc_y
px0 = cart_x * 256 / (2 * Math::PI )
py0 = cart_y * 256 / (2 * Math::PI )
tile_x = px0 * (2 ** zoom) / 256
tile_y = py0 * (2 ** zoom) / 256
{:x => tile_x.floor, :y => tile_y.floor}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment