psharma
psharma

Reputation: 1289

RoR: Check for a location within a radius of the other location, using google maps api

I am using google maps fo rails gem https://github.com/apneadiving/Google-Maps-for-Rails/ . I am trying to find out a way through which I can see if a location is inside the map circle. For instance

@circles_json = '[
 {"lng": -122.214897, "lat": 37.772323, "radius": 1000000},
]

'

This helps me create the circle. But how do I see if, lets say, 'Location B' is inside @circles_json ?

I was thinking of using the gem ruby geocoderhttp://www.rubygeocoder.com/. If so, how do I go ahead with that? I watched the railscasts. But it mostly depends on the local db. What I am looking for is to see if the two location entered in my database fall close to each other or not. In this case, in a 50M radius.

Any guidance is appreciated.

Thanks

Upvotes: 5

Views: 3738

Answers (1)

Substantial
Substantial

Reputation: 6682

To check if a point is within a circle, calculate the distance between the point and the circle's center. If the distance is greater than the radius, it is outside the circle. If the radius is greater than the distance, it is inside the circle.

Ruby Geocoder has a convenient method to calculate the distance between two points.

Geocoder::Calculations.distance_between(point1, point2)

To make this return true/false, use a simple comparator:

in_circle? = Geocoder::Calculations.distance_between(point1, point2) < radius

Read the documentation for help using #distance_between.

Upvotes: 10

Related Questions