2016-07-21 67 views

回答

3

,你需要经过像这样的基本步骤:

  1. 创建CLLocationmanager实例
  2. 指定一个代表与 适当的回调方法
  3. 检查,看是否“速“设置在回调中的CLLocation中,如果是 - 则表示您的速度为
  4. (可选)如果”速度“不是 集合,请尝试从las之间的距离计算它T更新的 和电流,通过差异时间戳分

    import CoreLocation 
    
    class locationDelegate: NSObject, CLLocationManagerDelegate { 
        var last:CLLocation? 
        override init() { 
         super.init() 
        } 
        func processLocation(_ current:CLLocation) { 
         guard last != nil else { 
          last = current 
          return 
         } 
         var speed = current.speed 
         if (speed > 0) { 
          print(speed) // or whatever 
         } else { 
          speed = last!.distance(from: current)/(current.timestamp.timeIntervalSince(last!.timestamp)) 
          print(speed) 
         } 
         last = current 
        } 
        func locationManager(_ manager: CLLocationManager, 
           didUpdateLocations locations: [CLLocation]) { 
         for location in locations { 
          processLocation(location) 
         } 
        } 
    } 
    
    var del = locationDelegate() 
    var lm = CLLocationManager(); 
    lm.delegate = del 
    lm.startUpdatingLocation() 
    
+0

人,什么是* *了今天的堆栈溢出编辑器?这取得了太多的编辑,使格式正确。 –

+0

我试图实现类似的这一点。你能解释一下差距吗?只是从速度上获得速度并使用公式计算它?因为当我打印两种速度方式他们有稍微不同的值 – Ilya

+0

我的问题是在真实的设备上,当我开车时,速度是不正确的。我在'didUpdateLocations'中使用了'locations.speed'。 – Ilya