Ios 计算行程时间和位置

Ios 计算行程时间和位置,ios,swift,cllocationmanager,cllocation,Ios,Swift,Cllocationmanager,Cllocation,我想在用户开始跟踪移动对象时,使用swift 2.2查找移动对象的移动时间。我编写了locationManager函数来跟踪移动对象的位置和移动距离,但我需要找到移动时间 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { userLocations.append(locations[0] as CLLocation) if startLoc

我想在用户开始跟踪移动对象时,使用swift 2.2查找移动对象的移动时间。我编写了locationManager函数来跟踪移动对象的位置和移动距离,但我需要找到移动时间

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    userLocations.append(locations[0] as CLLocation)

    if startLocation == nil {
        startLocation = locations.first! as CLLocation
    } else {
        let distance = startLocation.distanceFromLocation(locations.last! as CLLocation) / 1000
        let lastDistance = lastLocation.distanceFromLocation(locations.last! as CLLocation) / 1000;

        vartraveledDistance += lastDistance
        print( "\(startLocation)")
        print( "\(locations.last!)")
        print("FULL DISTANCE: \(traveledDistance)")

        print("STRAIGHT DISTANCE: \(distance)")
    }
    lastLocation = locations.last! as CLLocation
}


@IBAction func StartTrip(sender: AnyObject) {
    print("Start Trip")
    locationManager.startUpdatingLocation()
}

@IBAction func StopTrip(sender: AnyObject) {
    print("End Trip")
    locationManager.stopUpdatingLocation()
    traveledDistance.text = String(format:"%f km", self.vartraveledDistance)
}

我的答案将是obj-c,因为我还没有学会swift:)。但你会明白的

在您的此类中,您可以创建两个日期:

NSDate *startDate;
NSDate *stopDate;
然后在startTrip方法中初始化startDate:

startDate = [NSDate date]; 
在stopTrip中,初始化stopDate并计算行程时间:

stopDate = [NSDate date];
NSTimeInterval interval = [startDate timeIntervalSinceDate:stopDate];
NSLog (@"Travel time = %.0f seconds", interval);
另外,如果您使用的是MKMaps,则在MKDirections类中有一个方法

-(void)calculateETAWithCompletionHandler:(MKETAHandler)completionHandler

它允许您计算预计的旅行时间

您可以在设置startLocation时保存startDate,并使用NSDate TIMEINTERVALICENCEDATE方法计算旅行经过的时间:

首先将startDate对象和计算属性添加到视图控制器:

var startDate: NSDate!
var traveledTime: Double { return NSDate().timeIntervalSinceDate(startDate) }
然后,在设置您的起始位置后设置起始日期:

if startLocation == nil {
    startLocation = locations.first
    startDate = NSDate()
    // code ...
}
使用NSDateComponentsFormatter创建扩展以格式化您的时间:

extension NSTimeInterval {
    struct DateComponents {
        static let formatterPositional: NSDateComponentsFormatter = {
            let dateComponentsFormatter = NSDateComponentsFormatter()
            dateComponentsFormatter.unitsStyle = .Positional
            dateComponentsFormatter.allowedUnits = [.Hour,.Minute,.Second]
            dateComponentsFormatter.zeroFormattingBehavior = .DropLeading
            return dateComponentsFormatter
        }()
    }
    var positionalTime: String {
        return DateComponents.formatterPositional.stringFromTimeInterval(self) ?? ""
    }
}
现在您只需查看traveledTime:

print("travel elapsed time: \(traveledTime.positionalTime)")

最基本的:时间=距离/速度这里真正的问题是什么?在
StartTrip
中记下时间似乎很容易?