der_tom
(der_tom)
August 17, 2018, 7:59pm
1
hi,
i have a Task model with and address plus radius in miles (adress is being translated to lat/lng via geo-api-call).
Now i have a person at location x and i want to know if that person is within that circle of the TaskModel’s location plus radius.
does anyone know how to calculate that w/o using geo-api calls?
thx
tom
That's called the great-circle distance. It's not overly complex to implement by hand (I've done it in SQL and in app code in the past):
The great-circle distance, orthodromic distance, or spherical distance is the distance along a great circle.
It is the shortest distance between two points on the surface of a sphere, measured along the surface of the sphere (as opposed to a straight line through the sphere's interior). The distance between two points in Euclidean space is the length of a straight line between them, but on the sphere there are no straight lines. In spaces with curvature, straight lines are replaced by geodesics....
You might need to be careful about using floats, as they aren't perfectly accurate (funny rounding stuff can happen which could cause exceptions). Maybe use BigDecimal if doing your calculation with your own ruby code.
Hope that helps.
Phil
hassan
(Hassan Schroeder)
August 17, 2018, 9:34pm
3
ruby, google-maps
1. Do the math yourself, or
2. Use the "geokit" gem <= that'd be my choice
der_tom
(der_tom)
August 20, 2018, 1:47pm
4
yes, this seems to be working:
b = Geokit::Bounds.from_point_and_radius([c.latitude, c.longitude],c.distance)
p b.sw
from = [hdr.loclat, hdr.loclng]
to = [c.latitude, c.longitude]
if(b.contains?(from)) …
is this local calculation or does it make an API call?
hassan
(Hassan Schroeder)
August 20, 2018, 2:06pm
5
I would think it'd be local, but to be certain I'd either
1. look at the source code
2. perform the calculation in a console while disconnected from
any network
HTH,