提供获取当前速度的简单方法(实施速度计) [英] provide simple method to get current speed (implementing speedometer)

查看:297
本文介绍了提供获取当前速度的简单方法(实施速度计)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问我如何计算当前速度的示例,我正在开发我的第一个简单的应用程序ala速度计?

Could you please give me example how to calculate current "speed", I'm working on my first simple app ala speedometer?

我想使用 didUpdateToLocation

我还发现我需要使用公式 speed = distance / duration /

also I found that I need to use formula speed = distance / duration

对吗?

推荐答案

所需的基本步骤如下:


  1. 创建CLLocationmanager实例

  2. 使用
    适当的回调方法分配委托

  3. 检查是否在回调中的CLLocation上设置了速度,如果设置了-您的速度

  4. (可选),如果速度没有设置
    ,请尝试根据上次更新
    与当前更新之间的距离除以时间戳记的差异来计算它。

  1. Create a CLLocationmanager instance
  2. Assign a delegate with the appropriate callback method
  3. Check to see if the "speed" is set on the CLLocation in the callback, if it is - there's your speed
  4. (Optionally) if the "speed" isn't set, try calculating it from the distance between the last update and the current, divided by the difference in timestamps

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()


这篇关于提供获取当前速度的简单方法(实施速度计)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆