Skip to content

Instantly share code, notes, and snippets.

@piffall
Forked from rhee/mysql geo distance
Last active August 29, 2015 14:13
Show Gist options
  • Save piffall/537f2ad6d0eb8c2bdb96 to your computer and use it in GitHub Desktop.
Save piffall/537f2ad6d0eb8c2bdb96 to your computer and use it in GitHub Desktop.
-- http://dossy.org/2011/09/mysql-geo-distance-code-and-samples/
DELIMITER $$
DROP FUNCTION IF EXISTS geodist $$
CREATE FUNCTION geodist (
src_lat DECIMAL(9,6), src_lon DECIMAL(9,6),
dst_lat DECIMAL(9,6), dst_lon DECIMAL(9,6)
) RETURNS DECIMAL(6,2) DETERMINISTIC
BEGIN
SET @dist := 3956 * 2 * ASIN(SQRT(
POWER(SIN((src_lat - ABS(dst_lat)) * PI()/180 / 2), 2) +
COS(src_lat * PI()/180) *
COS(ABS(dst_lat) * PI()/180) *
POWER(SIN((src_lon - dst_lon) * PI()/180 / 2), 2)
));
RETURN @dist;
END $$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment