How to find out distance between coordinates?

前端 未结 10 1277
猫巷女王i
猫巷女王i 2020-12-12 15:23

I want to make it so that it will show the amount of distance between two CLLocation coordinates. Is there someway to do this without a complex math formula? If there isn\'t

10条回答
  •  执念已碎
    2020-12-12 16:05

    Swift 5.

    func calculateDistance(mobileLocationX:Double,mobileLocationY:Double,DestinationX:Double,DestinationY:Double) -> Double {
    
            let coordinate₀ = CLLocation(latitude: mobileLocationX, longitude: mobileLocationY)
            let coordinate₁ = CLLocation(latitude: DestinationX, longitude:  DestinationY)
    
            let distanceInMeters = coordinate₀.distance(from: coordinate₁)
    
            return distanceInMeters
        }
    

    use to

    let distance = calculateDistance("add parameters")
    

提交回复
热议问题