Method: Geocoder::Calculations#bounding_box

Defined in:
lib/geocoder/calculations.rb

#bounding_box(point, radius, options = {}) ⇒ Object

Returns coordinates of the southwest and northeast corners of a box with the given point at its center. The radius is the shortest distance from the center point to any side of the box (the length of each side is twice the radius).

This is useful for finding corner points of a map viewport, or for roughly limiting the possible solutions in a geo-spatial search (ActiveRecord queries use it thusly).

See Geocoder::Calculations.distance_between for ways of specifying the point. Also accepts an options hash:

  • :units - :mi or :km. Use Geocoder.configure(:units => …) to configure default units.



210
211
212
213
214
215
216
217
218
219
# File 'lib/geocoder/calculations.rb', line 210

def bounding_box(point, radius, options = {})
  lat,lon = extract_coordinates(point)
  radius  = radius.to_f
  [
    lat - (radius / latitude_degree_distance(options[:units])),
    lon - (radius / longitude_degree_distance(lat, options[:units])),
    lat + (radius / latitude_degree_distance(options[:units])),
    lon + (radius / longitude_degree_distance(lat, options[:units]))
  ]
end