提供获取当前速度的简单方法(实施速度计)

编程入门 行业动态 更新时间:2024-10-27 12:30:39
本文介绍了提供获取当前速度的简单方法(实施速度计)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请问我如何计算当前速度的示例,我正在开发我的第一个简单的应用程序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

对吗?

推荐答案

所需的基本步骤如下:

  • 创建CLLocationmanager实例
  • 使用适当的回调方法分配委托
  • 检查是否在回调中的CLLocation上设置了速度,如果设置了-您的速度
  • (可选),如果速度没有设置,请尝试根据上次更新与当前更新之间的距离除以时间戳记的差异来计算它。

  • Create a CLLocationmanager instance
  • Assign a delegate with the appropriate callback method
  • Check to see if the "speed" is set on the CLLocation in the callback, if it is - there's your speed
  • (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()

  • 更多推荐

    提供获取当前速度的简单方法(实施速度计)

    本文发布于:2023-06-04 21:18:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/505240.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:速度计   速度   简单   方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!