Calculate Total Traveled Distance iOS Swift

后端 未结 3 1399
[愿得一人]
[愿得一人] 2020-12-01 10:20

How can I calculate the total distance traveled use CoreLocation in Swift

I haven\'t been able to so far find any resources for how to do this in Swift for iOS 8,

3条回答
  •  广开言路
    2020-12-01 10:36

    Leo Dabus method could be used to calculate the geographical distance between your actual location and start one.

    In order to obtain the precise traveled distance, you have to update "traveledDistance" using the difference between the last position and the old one.

    This is my implementation:

    var startLocation:CLLocation!
    var lastLocation: CLLocation!
    var traveledDistance:Double = 0
    
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        if startLocation == nil {
            startLocation = locations.first as! CLLocation
        } else {
            let lastLocation = locations.last as! CLLocation
            let distance = startLocation.distanceFromLocation(lastLocation)
            startLocation = lastLocation
            traveledDistance += distance 
        }
    }
    

提交回复
热议问题