Skip to content

Instantly share code, notes, and snippets.

@maxp
Created August 27, 2017 04:59
Show Gist options
  • Save maxp/8e6330fb53f84abe35a26d965d053e27 to your computer and use it in GitHub Desktop.
Save maxp/8e6330fb53f84abe35a26d965d053e27 to your computer and use it in GitHub Desktop.
distance between two points on Earth, #geo
defn haversine
[{lon1 :longitude lat1 :latitude} {lon2 :longitude lat2 :latitude}]
(let [R 6372.8 ; kilometers
dlat (Math/toRadians (- lat2 lat1))
dlon (Math/toRadians (- lon2 lon1))
lat1 (Math/toRadians lat1)
lat2 (Math/toRadians lat2)
a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lat1) (Math/cos lat2)))]
(* R 2 (Math/asin (Math/sqrt a)))))
(haversine {:latitude 36.12 :longitude -86.67} {:latitude 33.94 :longitude -118.40})
;=> 2887.2599506071106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment