Anyone implemented the Haversine formula in B3D?

Community Forums/General Help/Anyone implemented the Haversine formula in B3D?

Blitzplotter(Posted 2011) [#1]
to calculate the distance between two lat & longs.... ?


BlitzSupport(Posted 2011) [#2]
I hadn't implemented it, or heard of it, but I was curious!


; Ported from http://www.movable-type.co.uk/scripts/latlong.html

Function Rad# (ang#)
	Return ang * (Pi / 180.0)
End Function

Function MapDistance# (lat1#, long1#, lat2#, long2#)

	R# = 6371 ; Earth's radius in km...
	
	dLat# = Rad (lat2# - lat1#)
	dLon# = Rad (long2# - long1#)
	
	lat1 = Rad (lat1)
	lat2 = Rad (lat2)
	
	a# = Sin (dLat / 2.0) * Sin (dLat / 2.0) + Sin (dLon / 2.0) * Sin (dLon / 2.0) * Cos (lat1) * Cos (lat2)
	c# = 2.0 * ATan2 (Sqr (a), Sqr (1.0 - a))
	Return R * c

End Function

; Dundee to Edinburgh!

Print MapDistance (56.3, -2.9, 55.95, -3.34)

MouseWait
End


The straight-line distance from Dundee to Edinburgh is 62.5 km:

http://www.mapcrow.info/Distance_between_Edinburgh_UK_and_Dundee_UK.html

It works for that case, anyway.


Matty(Posted 2011) [#3]
Good Morning all,

Just tested your function James in Monkey with my Android phone and it works well...see here for details:

http://www.monkeycoder.co.nz/Community/post.php?topic=1366&post=15570


BlitzSupport(Posted 2011) [#4]
Nice! I do want to play with GPS myself, so this'll come in handy when I eventually pick up a decent device, thanks.