Created
June 30, 2020 19:50
-
-
Save lassbon/55468b6eb06f26d795fee71daf330568 to your computer and use it in GitHub Desktop.
How to calculate distance between two latitude-longitude coordinates in MYSQL
This file contains hidden or 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
| CREATE FUNCTION `lat_lng_distance` (lat1 FLOAT, lng1 FLOAT, lat2 FLOAT, lng2 FLOAT) | |
| RETURNS FLOAT | |
| DETERMINISTIC | |
| BEGIN | |
| RETURN 6371 * 2 * ASIN(SQRT( | |
| POWER(SIN((lat1 - abs(lat2)) * pi()/180 / 2), | |
| 2) + COS(lat1 * pi()/180 ) * COS(abs(lat2) * | |
| pi()/180) * POWER(SIN((lng1 - lng2) * | |
| pi()/180 / 2), 2) )); | |
| END | |
| --Returns the distance in kilometers, assuming a earth radius of 6,371 km. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment