iPhone Compass GPS Direction

There is a standard “heading” or “bearing” equation that you can use – if you are at lat1,lon1, and the point you are interested in is at lat2,lon2, then the equation is:

heading = atan2( sin(lon2-lon1)*cos(lat2), cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(lon2-lon1))

This gives you a bearing in radians, which you can convert to degrees by multiplying by 180/π. The value is then between -180 and 180 degrees, so to get a standard compass bearing add 360 to any negative answers.

atan2 is a standard function related to arctan, that does the right thing for the four possible quadrants that your destination point could be in compared to where you are.

Leave a Comment