我已经使用 CoreMotionCMMotionManager motion.heading
一段时间了,似乎当我关闭应用程序并在一段时间后重新打开它时,我需要重新校准指南针。例如,我的屏幕上有一个指向 motion.heading 的箭头。最初,我将手机指向 100 米外的可见位置,它直接指向该位置。一段时间后,我可以将手机以完全相同的方式定位,但指南针箭头不同。
因此在实践中,我重新创建了所有我能创建的变量,包括手机的方向、地标位置以及我相对于地标握住手机的位置,但航向有时会报告不同的值。
我目前使用此代码来获取标题信息:
import CoreMotion
import SwiftUI
class Compass: ObservableObject {
private var motionManager: CMMotionManager
@Published var heading: Double = 0.0
init() {
self.motionManager = CMMotionManager()
self.startUpdates()
}
private func startUpdates() {
if motionManager.isDeviceMotionAvailable {
motionManager.deviceMotionUpdateInterval = 0.1
motionManager.startDeviceMotionUpdates(to: .main) { [weak self] (motion, error) in
guard let motion = motion else { return }
self?.heading = heading.truncatingRemainder(dividingBy: 360) // normalize for 360 degrees of possible heading rotation
}
}
}
箭头仅显示指向地标的距离公式,其中包含航向。但航向值会发生变化。有没有比这更好或更一致的方式来获取手机指向的方向?比如使用磁北或类似的东西?
指南针应用程序总是显示相同的关于罗盘北方的内容,那么为什么当指南针应用程序仍然正常工作时,motion.heading 有时会显示不同的值?