steveauckly
25th September 2009, 19:29
Anyone write a function to calculate the distance between two GPS (lat/long) coordinates?
steveauckly
25th September 2009, 23:24
Found some code on the internet and adapted it for Baan, seems to work:
function extern double gpscalc.distance( domain ttsql.doub lat1,
domain ttsql.doub long1,
domain ttsql.doub lat2,
domain ttsql.doub long2)
{
|Returns distance in miles given input of two lat/long pairs
domain ttsql.doub radcalc, distance, calcdist, earthrad
radcalc = 57.29577951 |180/pi (to use in conversion from degrees to radians)
earthrad = 3963.1 |assumed radius of earth in miles
if not double.cmp(lat1,lat2,0.00001) and
not double.cmp(long1,long2,0.00001) then
|Points are the same, distance = 0
distance = 0.0
else
|convert degrees to radians
lat1 = lat1/radcalc
long1 = long1/radcalc
lat2 = lat2/radcalc
long2 = long2/radcalc
calcdist = (sin(lat1) * sin(lat2)) + (cos(lat1) * cos(lat2) * cos(long2 - long1))
if double.cmp(calcdist,1.0,0.00001) > 0 then
distance = earthrad * acos(1)
else
distance = earthrad * acos(calcdist)
endif
endif
return(distance)
}
vahdani
26th September 2009, 14:05
Hi Steve,
thank you for the code! Just out of curiosity: What are you using this for?
steveauckly
28th September 2009, 15:10
We do a lot of work with subcontractors all over the US. When we enter an order, this helps us figure out which subcontractor we have close to where the work site is.