I am looking to write a two functions manage GPS/heading inputs
The first function will take the user sGPS(lat,lon) and another set of cords(lat,lon) and return a heading (compass direction for the second cocords from the users position).
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)) -- 6378 km is the earth's radius at the equator. -- 6357 km would be the radius at the poles (earth isn't a perfect circle). -- Thus, high latitude distances will be slightly overestimated -- To get miles, use 3963 as the constant (equator again) local d = 6378 \* c return d end
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)) -- 6378 km is the earth's radius at the equator. -- 6357 km would be the radius at the poles (earth isn't a perfect circle). -- Thus, high latitude distances will be slightly overestimated -- To get miles, use 3963 as the constant (equator again) local d = 6378 \* c return d end