Skip to content

Instantly share code, notes, and snippets.

@zackham
Created July 2, 2013 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zackham/5911093 to your computer and use it in GitHub Desktop.
Save zackham/5911093 to your computer and use it in GitHub Desktop.
def self.tiles_for(lat, lng, search_radius)
adj_lat = (lat + 90).floor
adj_lng = (lng + 180).floor
lat_interval = 110_574 # this is at the poles, its around 111km at the equator so we're using the shorter one - not worth calculating a more accurate value based on lat
# this one is from here: http://en.wikipedia.org/wiki/Longitude
# specifically: PI / 180 cos(theta) * N(theta), and we're using a fixed value
# of N(theta) because its close enough. Basically it is 0 at the poles
# and 68 miles at the equator. We'll allow a minimum of 10km though, which is
# basically at the poles, because I dont want to use a ton of tiles in
# the search
lng_interval = (Math.cos(lat * Math::PI / 180) * 6_378_137 * Math::PI / 180)
lng_interval = 10_000 if lng_interval < 10_000
lat_deg_radius = (search_radius / lat_interval).ceil
lng_deg_radius = (search_radius / lng_interval).ceil
tiles = []
(adj_lng - lng_deg_radius).upto(adj_lng + lng_deg_radius) do |x|
(adj_lat - lat_deg_radius).upto(adj_lat + lat_deg_radius) do |y|
tiles << "#{x}x#{y}"
end
end
tiles
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment