Page 1 of 1

GPS calculation

Posted: Tuesday 21 March 2017 23:55
by phoenixblue
Hello,

Question, does someone have an example calculation to calculate the distance between 2 GPS coordinates in Lua so when the result is for example lower then 20km I can trigger an notification.

Regards

Re: GPS calculation

Posted: Wednesday 22 March 2017 12:49
by emme
a siple search on google found this:

Code: Select all

local function geo_distance(lat1, lon1, lat2, lon2)
  if lat1 == nil or lon1 == nil or lat2 == nil or lon2 == nil then
    return nil
  end
  local dlat = math.rad(lat2-lat1)
  local dlon = math.rad(lon2-lon1)
  local sin_dlat = math.sin(dlat/2)
  local sin_dlon = math.sin(dlon/2)
  local a = sin_dlat * sin_dlat + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * sin_dlon * sin_dlon
  local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
 
  local d = 6378 * c
  return d
end
a deeper search (the second link :P) found this: https://coronalabs.com/blog/2015/01/06/ ... ap-points/

Re: GPS calculation

Posted: Wednesday 22 March 2017 13:09
by phoenixblue
Thanks for the tip ;-)
Sometimes it's also handy to check something else besides the forum.
Thanks.