Skip to content

Instantly share code, notes, and snippets.

@rhee
Created January 14, 2015 07:45
Show Gist options
  • Save rhee/350fca09a5b577e33d12 to your computer and use it in GitHub Desktop.
Save rhee/350fca09a5b577e33d12 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