This MySQL Stored Function computes the initial bearing — the compass heading to follow — when moving from one point to another on the surface of the earth. Here’s a web site describing this. http://www.movable-type.co.uk/scripts/latlong.html
DELIMITER $$
DROP FUNCTION IF EXISTS `initial_bearing`$$
CREATE FUNCTION `initial_bearing`(
lat1 DOUBLE, lon1 DOUBLE,
lat2 DOUBLE, lon2 DOUBLE
) RETURNS DOUBLE
NO SQL
DETERMINISTIC
COMMENT 'Returns the initial bearing, in degrees, to follow the great circle route
from point (lat1,lon1), to point (lat2,lon2)'
BEGIN
RETURN (360.0 +
DEGREES(ATAN2(
SIN(RADIANS(lon2-lon1))*COS(RADIANS(lat2)),
COS(RADIANS(lat1))*SIN(RADIANS(lat2)) - SIN(RADIANS(lat1))*COS(RADIANS(lat2))*
COS(RADIANS(lon2-lon1))
))
) % 360.0;
END$$
DELIMITER ;