Search code examples
mysqlruby-on-railsrails-geocoder

Find all values from X within Y radius from a given location (longitude,latitude)


I have a model named Offering and User

user has_many offerings.

Every offering has two fields long, lat which contains the longitude latitude of the offering, where it is created.

Suppose, A user's location is 2.323, -32.344. Now I want to show him the offerings which is within 10 miles radius from his location.

I'm using MySQL, engine= INNODB

Have any idea?


Solution

  • Geocoder does provide such kind of scopes:

    # venues within 10 miles of 'Omaha, NE, US'
    Offering.near('Omaha, NE, US', 10)    
    
    # venues within 10 miles of [40.71, 100.23]
    Offering.near([40.71, 100.23], 10)    
    

    Doc here