MySQL function to calculate the distance between two latitude and latitude GPS pairs

By | April 10, 2014
CREATE DEFINER=`root`@`localhost` FUNCTION `FIND_DISTANCE`(`src_latitude` float,`src_longitude` float, `dest_latitude` float,`dest_longitude` float) RETURNS int(11)
BEGIN
	/*
	This function takes four parameterss, two sets of latitude and latitude pairs and returns the distance between them in miles.

  Example use
  SELECT * , FIND_DISTANCE(home_latitude, home_longitude, 52.5, -1) AS distance FROM users WHERE deleted = 0;
	*/
	RETURN ( 3959 * acos( cos( radians(src_latitude) ) * cos( radians( dest_latitude ) ) * cos( radians(dest_longitude) - radians(src_longitude)) + sin(radians(src_latitude)) * sin( radians(dest_latitude))));
END

 

Uses Haversine formula

Independently verified result metric to me miles by usingĀ http://www.movable-type.co.uk/scripts/latlong.html with the same coordinates. Distance on that site is shown as KM, but converting to miles resulted in the same result as this function.