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

Expand on within_bounding_box documentation.

Also move it to a new "Advanced Querying" section.
parent 4e211411
No related branches found
No related tags found
No related merge requests found
......@@ -118,16 +118,6 @@ To find objects by location, use the following scopes:
Venue.geocoded # venues with coordinates
Venue.not_geocoded # venues without coordinates
You can also restrict the bounds used in location queries via the <tt>within_bounding_box</tt> scope. This
can have a dramatic effect on query performance, especially when used in conjunction with indexes on the lat,
lng columns.
distance = 20
center_point = [40.71, 100.23]
box = Geocoder::Calculations.bounding_box(center_point, distance)
Venue.near(center_point, distance).within_bounding_box(box, distance) # same results as above, potentially faster
Venue.within_bounding_box(box, distance) # even faster query, but no distance ordering
With geocoded objects you can do things like this:
obj.nearbys(30) # other objects within 30 miles
......@@ -211,6 +201,20 @@ For reverse geocoding you can also specify an alternate name attribute where the
reverse_geocoded_by :coordinates, :address => :loc # MongoDB
== Advanced Querying
When querying for objects (if you're using ActiveRecord) you can also look within a square rather than a radius (circle) by using the <tt>within_bounding_box</tt> scope:
distance = 20
center_point = [40.71, 100.23]
box = Geocoder::Calculations.bounding_box(center_point, distance)
Venue.within_bounding_box(box, distance)
This can also dramatically improve query performance, especially when used in conjunction with indexes on the latitude/longitude columns. Note, however, that returned results do not include +distance+ and +bearing+ attributes. If you want to improve performance AND have access to distance and bearing info, use both scopes:
Venue.near(center_point, distance).within_bounding_box(box, distance)
== Advanced Geocoding
So far we have looked at shortcuts for assigning geocoding results to object attributes. However, if you need to do something fancy you can skip the auto-assignment by providing a block (takes the object to be geocoded and an array of <tt>Geocoder::Result</tt> objects) in which you handle the parsed geocoding result any way you like, for example:
......
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