[AccessD] Weird problem with lat/long distance function

Mark A Matte markamatte at hotmail.com
Thu Aug 23 12:04:34 CDT 2012


Hello All,
I have been using the following for some time...today I am comparing coordinates that are very close...and I am getting 'weird' results/errors.(A2007)
I entered the lat/long numbers in the code below to show the behavior I am seeing.
With these coordinates...the CosArc = 1...but the IF statement below executes the ELSE.
Unless the absolute value of 1 is NOT 1...why would this not work?
Thanks,,,  Mark
'If Abs(CosArc) = 1 Then'    Arc = PI'Else
Public Function Distance( _    ByVal Latitude1 As Double, _    ByVal Longitude1 As Double, _    ByVal Latitude2 As Double, _    ByVal Longitude2 As Double, _    Optional Miles As Boolean) As Double'courtesy Lyle Fairfield'assumes we are getting latitude-longitude'as degrees with fractions expresed as decimals'if minutes-seconds then use Sexagesimal function to convertDim CosArc As DoubleDim Arc As DoubleLatitude1 = Radians(41.70900431)Longitude1 = Radians(-83.585444)Latitude2 = Radians(41.7090043053031)Longitude2 = Radians(-83.5854439996183)'Latitude1 = Radians(Latitude1)'Longitude1 = Radians(Longitude1)'Latitude2 = Radians(Latitude2)'Longitude2 = Radians(Longitude2)
CosArc = (Sin(Latitude1) * Sin(Latitude2)) + _    (Cos(Latitude1) * Cos(Latitude2) * Cos(Longitude1 - Longitude2))
'***PROBLEM HERE***If Abs(CosArc) = 1 Then    Arc = PIElse    Arc = Atn(-CosArc / Sqr(-CosArc * CosArc + 1)) + 2 * Atn(1)End IfDistance = Arc / PI / 2 * CircumferenceIf Miles = True Then Distance = Distance * MilesPerKilometerDistance = Round(Distance, 2)End Function
Private Function Radians(ByVal degrees As Double) As Double    Radians = PI * degrees / 180End Function 		 	   		  


More information about the AccessD mailing list