Skip to content

Instantly share code, notes, and snippets.

@sethmclean
Created January 12, 2015 10:09
Show Gist options
  • Select an option

  • Save sethmclean/eaef874610024fddfe14 to your computer and use it in GitHub Desktop.

Select an option

Save sethmclean/eaef874610024fddfe14 to your computer and use it in GitHub Desktop.
SET
@orig_latitude = 32.867073,
@orig_longitude = -96.769410,
@radius = 10
;
SELECT *
FROM (
SELECT
`geonameid`, `name`,
3956 * ACOS(COS(RADIANS(@orig_latitude)) * COS(RADIANS(`latitude`)) * COS(RADIANS(@orig_longitude) - RADIANS(`longitude`)) + SIN(RADIANS(@orig_latitude)) * SIN(RADIANS(`latitude`))) AS `distance`
FROM `geoname`
WHERE
`latitude`
BETWEEN @orig_latitude - (@radius / 69)
AND @orig_latitude + (@radius / 69)
AND `longitude`
BETWEEN @orig_longitude - (@radius / (69 * COS(RADIANS(@orig_latitude))))
AND @orig_longitude + (@radius / (69* COS(RADIANS(@orig_latitude))))
) r
WHERE `distance` < @radius
ORDER BY `distance` ASC
;
@SuN-80

SuN-80 commented Mar 16, 2015

Copy link
Copy Markdown

Thanks mate.

@KalanaPerera

KalanaPerera commented Aug 20, 2017

Copy link
Copy Markdown

Thank you very much, this is just took milliseconds to get the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment