Skip to content

Instantly share code, notes, and snippets.

@chessworld
Created March 19, 2012 04:44
Show Gist options
  • Select an option

  • Save chessworld/2095515 to your computer and use it in GitHub Desktop.

Select an option

Save chessworld/2095515 to your computer and use it in GitHub Desktop.
MySQL stored procedure for distance calculation
create function distance(lat1 real, lng1 real, lat2 real, lng2 real) returns real no sql
return ATAN2(SQRT(POW(COS(RADIANS(lat2)) * SIN(RADIANS(lng1 - lng2)), 2) +
POW(COS(RADIANS(lat1)) * SIN(RADIANS(lat2)) - SIN(RADIANS(lat1)) *
COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)), 2)),
(SIN(RADIANS(lat1)) * SIN(RADIANS(lat2)) + COS(RADIANS(lat1)) *
COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)))) * 6372.795;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment