How to find out distance between coordinates?

前端 未结 10 1305
猫巷女王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:06

    func calculateDistanceInMiles(){
    
        let coordinate₀ = CLLocation(latitude:34.54545, longitude:56.64646)
        let coordinate₁ = CLLocation(latitude: 28.4646, longitude:76.65464)
        let distanceInMeters = coordinate₀.distance(from: coordinate₁)
        if(distanceInMeters <= 1609)
        {
            let s =   String(format: "%.2f", distanceInMeters)
            self.fantasyDistanceLabel.text = s + " Miles"
        }
        else
        {
            let s =   String(format: "%.2f", distanceInMeters)
            self.fantasyDistanceLabel.text = s + " Miles"
    
        }
    }
    

提交回复
热议问题