Snippets

Tuomas Hietanen Calculate distance between two GPS latitude-longitude points.

Created by Tuomas Hietanen last modified
open System

let ``calculate distance`` (p1Latitude,p1Longitude) (p2Latitude,p2Longitude) =
    let r = 6371.0; // km
    let dLat = (p2Latitude - p1Latitude) * Math.PI / 180.0
    let dLon = (p2Longitude - p1Longitude) * Math.PI / 180.0
    let lat1 = p1Latitude * Math.PI / 180.0
    let lat2 = p2Latitude * Math.PI / 180.0

    let a = Math.Sin(dLat/2.0) * Math.Sin(dLat/2.0) +
            Math.Sin(dLon/2.0) * Math.Sin(dLon/2.0) * Math.Cos(lat1) * Math.Cos(lat2)
    let c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0-a))
    r * c

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.