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,
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
}
}