【问题标题】:Calculate travel time CLLocation Swift计算行程时间 CLLocation Swift
【发布时间】:2016-04-04 13:54:23
【问题描述】:

当用户开始跟踪对象时,我想使用 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)
}

【问题讨论】:

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

标签: ios swift cllocationmanager cllocation


【解决方案1】:

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

首先向视图控制器添加一个 startDate 对象和一个计算属性:

var startDate: NSDate!
var traveledTime: Double { return NSDate().timeIntervalSinceDate(startDate) }

然后在设置你的 startLocation 后设置 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)")

【讨论】:

    【解决方案2】:

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

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

    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
    

    它可以让您计算预计的旅行时间

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2014-04-10
      • 1970-01-01
      • 2014-07-13
      • 2016-09-02
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多