Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active May 14, 2016 16:27
Show Gist options
  • Select an option

  • Save Kcko/05eb59aa4fc4445a6ef3afc6ecd16ff5 to your computer and use it in GitHub Desktop.

Select an option

Save Kcko/05eb59aa4fc4445a6ef3afc6ecd16ff5 to your computer and use it in GitHub Desktop.
// 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) ));
-- 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