Skip to content

Instantly share code, notes, and snippets.

@zafe
Created March 16, 2017 05:08
Show Gist options
  • Save zafe/cc0ee853f791187efc9001e0c10aac73 to your computer and use it in GitHub Desktop.
Save zafe/cc0ee853f791187efc9001e0c10aac73 to your computer and use it in GitHub Desktop.
MySQL function to determine the distance between two points considering Earth's spherical surface.
DELIMITER $$
-- Returns value in Km (Kilometers)
CREATE FUNCTION distance_points (lat1 FLOAT, lng1 FLOAT, lat2 FLOAT, lng2 FLOAT)
RETURNS FLOAT
DETERMINISTIC
BEGIN
RETURN (acos(sin(radians(lat1)) * sin(radians(lat2)) +
cos(radians(lat1)) * cos(radians(lat2)) *
cos(radians(lng1) - radians(lng2))) * 6371);
END$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment