Skip to content

Instantly share code, notes, and snippets.

@derencius
Created April 20, 2012 22:04
Show Gist options
  • Save derencius/2432213 to your computer and use it in GitHub Desktop.
Save derencius/2432213 to your computer and use it in GitHub Desktop.
latitude/longitude distance function - for mysql (original source: http://derickrethans.nl/spatial-indexes-mysql.html - syntax changed to fit in 1 line for easy use in a rails migration).
CREATE FUNCTION distance (latA double, lonA double, latB double, LonB double)
RETURNS double DETERMINISTIC
RETURN 2 * ASIN(SQRT((SIN((radians(latA) - radians(latB))/2) * SIN((radians(latA) - radians(latB))/2) +
COS(radians(latA)) * COS(radians(latB)) * SIN((radians(lonA) - radians(LonB))/2)*SIN((radians(lonA) - radians(LonB))/2)))) * 6371.01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment