Last active
September 6, 2016 14:53
-
-
Save carloscarucce/f864ded84dad6ce99c4f7669c83236a6 to your computer and use it in GitHub Desktop.
Retus the distance based in two geo points (latitude/longitude)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER $$ | |
CREATE FUNCTION haversine_geo_distance (lat1 REAL, lon1 REAL, lat2 REAL, lon2 REAL) | |
RETURNS REAL | |
BEGIN | |
DECLARE RR LONG; | |
DECLARE o1 REAL; | |
DECLARE o2 REAL; | |
DECLARE Ao REAL; | |
DECLARE AA REAL; | |
DECLARE a REAL; | |
DECLARE c REAL; | |
SET RR = 6371000; | |
SET o1 = RADIANS(lat1); | |
SET o2 = RADIANS(lat2); | |
SET Ao = RADIANS(lat2 - lat1); | |
SET AA = RADIANS(lon2 - lon1); | |
SET a = SIN(Ao/2) * SIN(Ao/2) + | |
COS(o1) * COS(o2) * | |
SIN(AA/2) * SIN(AA/2); | |
SET c = 2 * ATAN2(SQRT(a), SQRT(1-a)); | |
RETURN RR * c; | |
END $$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment