Last active
May 14, 2016 16:27
-
-
Save Kcko/05eb59aa4fc4445a6ef3afc6ecd16ff5 to your computer and use it in GitHub Desktop.
From http://blog.bobbyallen.me/2015/04/04/get-nearest-places-from-mysql-with-latitude-and-longditude/
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
| // Praha, Drazdany | |
| SET @lat1 = 50.0598058; | |
| SET @lat2 = 51.0769658; | |
| SET @lng1 = 14.3255396; | |
| SET @lng2 = 13.6325016; | |
| select | |
| 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) )); |
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
| -- Set your users current location in LAT/LONG here | |
| set @lat=51.891648; | |
| set @lng=0.244799; | |
| -- The main SQL query that returns the closest 5 airports. | |
| SELECT id, icao, lat, lng, 111.045 * DEGREES(ACOS(COS(RADIANS(@lat)) | |
| * COS(RADIANS(lat)) | |
| * COS(RADIANS(lng) - RADIANS(@lng)) | |
| + SIN(RADIANS(@lat)) | |
| * SIN(RADIANS(lat)))) | |
| AS distance_in_km | |
| FROM airports | |
| ORDER BY distance_in_km ASC | |
| LIMIT 0,5; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment