Skip to content
Snippets Groups Projects
Commit 833fcf3c authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge branch 'master' of git://github.com/gavinhughes/geocoder into gavinhughes-master

parents 6dfed891 adba1b5c
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,25 @@ module Geocoder::Store
where(:id => false) # no results if no lat/lon given
end
}
##
# Find all objects within the area of a given bounding box.
# Bounds must be an array of locations specifying the southwest
# corner followed by the northeast corner of the box
# (<tt>[[sw_lat, sw_lon], [ne_lat, ne_lon]]</tt>).
#
scope :within_bounding_box, lambda{ |bounds|
sw_lat, sw_lng, ne_lat, ne_lng = bounds.flatten if bounds
return where(:id => false) unless sw_lat && sw_lng && ne_lat && ne_lng
spans = "latitude BETWEEN #{sw_lat} AND #{ne_lat} AND "
spans << if sw_lng > ne_lng # Handle a box that spans 180
"longitude BETWEEN #{sw_lng} AND 180 OR longitude BETWEEN #{ne_lng} and -180"
else
"longitude BETWEEN #{sw_lng} AND #{ne_lng}"
end
{ :conditions => spans }
}
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment