Calculate distance between 2 GPS coordinates

前端 未结 29 3556
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

29条回答
  •  攒了一身酷
    2020-11-22 00:05

    you can find a implementation of this (with some good explanation) in F# on fssnip

    here are the important parts:

    
    let GreatCircleDistance<[<Measure>] 'u> (R : float<'u>) (p1 : Location) (p2 : Location) =
        let degToRad (x : float<deg>) = System.Math.PI * x / 180.0<deg/rad>
    
        let sq x = x * x
        // take the sin of the half and square the result
        let sinSqHf (a : float<rad>) = (System.Math.Sin >> sq) (a / 2.0<rad>)
        let cos (a : float<deg>) = System.Math.Cos (degToRad a / 1.0<rad>)
    
        let dLat = (p2.Latitude - p1.Latitude) |> degToRad
        let dLon = (p2.Longitude - p1.Longitude) |> degToRad
    
        let a = sinSqHf dLat + cos p1.Latitude * cos p2.Latitude * sinSqHf dLon
        let c = 2.0 * System.Math.Atan2(System.Math.Sqrt(a), System.Math.Sqrt(1.0-a))
    
        R * c
    

提交回复
热议问题