lat lng + radius -> calculate in boundaries of

48 views
Skip to first unread message

tom

unread,
Aug 17, 2018, 3:59:45 PM8/17/18
to Ruby on Rails: Talk
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

Phil Edelbrock

unread,
Aug 17, 2018, 5:09:38 PM8/17/18
to rubyonra...@googlegroups.com
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):

https://en.wikipedia.org/wiki/Great-circle_distance

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 Schroeder

unread,
Aug 17, 2018, 5:34:35 PM8/17/18
to rubyonrails-talk
On Fri, Aug 17, 2018 at 12:59 PM, tom <toma...@gmail.com> wrote:

> does anyone know how to calculate that w/o using geo-api calls?

https://stackoverflow.com/questions/12966638/how-to-calculate-the-distance-between-two-gps-coordinates-without-using-google-m

1. Do the math yourself, or
2. Use the "geokit" gem <= that'd be my choice :-)

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

tom

unread,
Aug 20, 2018, 9:48:31 AM8/20/18
to Ruby on Rails: Talk
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?



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yBFps9wfXqiRTVXgxLBdwyo6WK7%3DO6vAVNgJpeMem-nbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Hassan Schroeder

unread,
Aug 20, 2018, 10:06:49 AM8/20/18
to rubyonrails-talk
On Mon, Aug 20, 2018 at 6:47 AM, tom <toma...@gmail.com> wrote:
> b = Geokit::Bounds.from_point_and_radius([c.latitude,
> c.longitude],c.distance)

> is this local calculation or does it make an API call?

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,
Reply all
Reply to author
Forward
0 new messages