Skip to content

Instantly share code, notes, and snippets.

@thomascrepain
Created October 1, 2015 11:34
Show Gist options
  • Save thomascrepain/6cbe641981341446f91c to your computer and use it in GitHub Desktop.
Save thomascrepain/6cbe641981341446f91c to your computer and use it in GitHub Desktop.
Latitude/Longitude

Calculate distance, bearing and more between Latitude/Longitude points http://www.movable-type.co.uk/scripts/latlong.html

-- Spherical Law of Cosines: calculate distance to other points

SET @lat = 51.053283, @lon = 3.720359;
SELECT *
FROM (
       SELECT
         *,
         (
           (
             (
               acos(
                   sin((@lat * pi() / 180)) * sin((`latitude` * pi() / 180)) +
                   cos((@lat * pi() / 180)) * cos((`latitude` * pi() / 180)) * cos(((@lon - `longitude`) * pi() / 180))
               )
             ) * 180 / pi()
           ) * 60 * 1.1515 * 1.609344
         ) AS distance
       FROM `cms_items_dealers`) myTable
ORDER BY distance ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment