Created
March 17, 2017 09:48
-
-
Save Ahrengot/e91881252364ee81b38d2f3487cdfd73 to your computer and use it in GitHub Desktop.
Haversine formula for calculating distance between two lat/lon coordinate sets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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