GPS calculation

Moderator: leecollings

Post Reply
User avatar
phoenixblue
Posts: 76
Joined: Friday 25 November 2016 11:20
Target OS: Windows
Domoticz version:
Contact:

GPS calculation

Post 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
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: GPS calculation

Post 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/
The most dangerous phrase in any language is:
"We always done this way"
User avatar
phoenixblue
Posts: 76
Joined: Friday 25 November 2016 11:20
Target OS: Windows
Domoticz version:
Contact:

Re: GPS calculation

Post by phoenixblue »

Thanks for the tip ;-)
Sometimes it's also handy to check something else besides the forum.
Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest