user984621
user984621

Reputation: 48453

Rails - geocoder: how to run ".near" method for multiple locations in the model?

I am trying to use geocoder to find all objects within a certain radius. Below is an example from the official Github site:

Venue.near([40.71, -100.23], 50)

This query will return me all venues that is in the 50-miles radius from the given location (40.71, -100.23).

However, in my case, the model Venue (table venues) has these columns representing 3 possible locations:

- latitude1
- longitude1
- latitude2
- longitude2
- latitude3
- longitude3

How do I force geocoder to look into all these 3 locations? Here, Venue.near([40.71, -100.23], 50) instead of looking only into

- latitude1
- longitude1

I'd need to check all 3 coordinations (maybe the 1st coordination isn't within the given radius from [40.71, -100.23], but the 2nd one might be - and in that case, it's a match). How do I achieve that?

Upvotes: 0

Views: 764

Answers (1)

ostapische
ostapische

Reputation: 1592

Please see Advanced Database Queries section.

If you store multiple sets of coordinates for each object, you can specify latitude and longitude columns to use for a search:

Venue.near("Paris", 50, latitude: :secondary_latitude, longitude: :secondary_longitude)

In this case you need to do 3 queries:

Venue.near([40.71, -100.23], 50, latitude: :latitude1, longitude: :longitude1)
Venue.near([40.71, -100.23], 50, latitude: :latitude2, longitude: :longitude2)
Venue.near([40.71, -100.23], 50, latitude: :latitude3, longitude: :longitude3)

Don't forget to remove duplicates.

Upvotes: 0

Related Questions