swift5cllocationxcode12cllocationdistance

Add some calculation to CLLocationDistance


Here is my problem I can print in console double value (aka the distance) but I can't take a double and add the distance in a table view for exemple.

if previousLocation == nil {
            previousLocation = locations.first
        } else {
            guard let latest = locations.first else { return }
            let distanceInMeters = previousLocation?.distance(from: latest) ?? 0
            print("Distance in meters: \(distanceInMeters)")
            guard var unwrappedPaceNumber = paceNumber.first else { return }
            
            let total = unwrappedPaceNumber += "\(distanceInMeters)"
            paceNumber[0] = "\(total)"
            tableView.reloadData()
            
            previousLocation = latest
        }

Solution

  • I finally ended up like that If this can help

     if previousLocation == nil {
                previousLocation = locations.first
            } else {
                guard let latest = locations.first else { return }
                let distanceInMeters = previousLocation?.distance(from: latest) ?? 0
                var distanceRounded = distanceInMeters.rounded()
                print("Distance in meters: \(distanceRounded)")
                let unwrappedPaceNumber = paceNumber[0]
                distanceRounded += Double(unwrappedPaceNumber)!
                paceNumber[0] = "\(distanceRounded)"
                print(paceNumber[0])
                tableView.reloadData()
    
                previousLocation = latest
            }