Calculate Distance, Bearing and Location for use with Geolocation Powered AR

Every time I have to do something that involves math I end up curled up in a corner, sobbing softly and hoping for it all to just go away.

For those who share my fear of algebra I’ve compiled a little PHP library that contains the most important functions when you need to calculate GPS locations and bearings for example for use in Augmented Reality applications.
It’s available for download here.

distanceBetweenCoords($lat1, $lon1, $lat2, $lon2)
Calculates the distance between 2 GPS coordinates using the Havesine formula.

bearingBetweenCoords($lat1, $lon1, $lat2, $lon2)
Calculates the initial bearing (angle) between 2 GPS coordinates. Mind you that the bearing changes during the travel from one point to the other.

mirrorBearing($iBearing[, $sAxis])
Mirrors a bearing over the x or y axis. By default the axis is ‘y’.

calcLatLong($lat, $lon, $distance, $bearing)
Calculates the coordinates of a point that is situated “distance” meters from the given lat/lon travelling in the direction of “bearing”.
Note that I’m not very sure how correct this calculation is although it worked fine for me.

Some of these formulas were ported from Javascript to PHP and originate from http://www.movable-type.co.uk/scripts/latlong.html

This entry was posted in General, tutorial and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>