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 geocoder
http://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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With