Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Created March 17, 2017 09:48
Show Gist options
  • Save Ahrengot/e91881252364ee81b38d2f3487cdfd73 to your computer and use it in GitHub Desktop.
Save Ahrengot/e91881252364ee81b38d2f3487cdfd73 to your computer and use it in GitHub Desktop.
Haversine formula for calculating distance between two lat/lon coordinate sets.
distanceInKm : Float -> Float -> Float -> Float -> String
distanceInKm lon1 lat1 lon2 lat2 =
let
-- Radius of The Earth in km
-- Use 3958 for miles
earthRadius =
6371
dLat =
degrees (lat2 - lat1)
dLon =
degrees (lon2 - lon1)
haversine =
(sin <| dLat / 2)
* (sin <| dLat / 2)
+ (cos <| degrees lat1)
* (cos <| degrees lat2)
* (sin <| dLon / 2)
* (sin <| dLon / 2)
c =
2 * (atan2 (sqrt haversine) (sqrt 1 - haversine))
in
(earthRadius * c) ++ " km"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment