【问题标题】:CoreMotion based compass基于 CoreMotion 的指南针
【发布时间】:2016-08-06 09:48:21
【问题描述】:

我想使用 CoreMotion(如果可能,不要使用 CoreLocation)计算指南针指向真北的方向。我找到了一些公式,但仅在手机平放在桌子上时才有效。有人可以帮我找到一种计算指南针航向的方法,该方法适用于指南针应用程序等每个设备位置吗?

【问题讨论】:

  • 您不使用 CoreLocation 的原因是什么?
  • 我正在制作一个 AR 应用程序,它已经使用 CoreMotion 来显示对象。我需要这个指南针用于雷达视图。我注意到,当我使用 CoreLocation trueHeading 时,有时雷达上的点会与我在 AR 视图上看到的有点偏移。可能会使用一些额外的过滤来计算 CoreLocation 的航向。无论如何,如果这可以从核心运动姿态获得航向,那么我认为这个问题不会存在。

标签: ios core-motion compass-geolocation


【解决方案1】:

我找到了答案。这就是我从 CoreMotion 以弧度获得指南针航向的方式:

guard let a = motionManager.deviceMotion?.attitude, g = motionManager.deviceMotion?.gravity else { return }

let deviceAngle = { () -> Double in
    switch interfaceOrientation {
    case .LandscapeLeft:  return -M_PI_2 + atan2(g.x, g.y)
    case .LandscapeRight: return  M_PI_2 + atan2(g.x, g.y)
    default:              return  M_PI   + atan2(g.x, g.y)
    }
}()

// Calculate Heading.
let cA = cos(a.yaw), sA = sin(a.yaw), sB = sin(a.pitch), cG = cos(a.roll), sG = sin(a.roll)
let rA = cA * sG + sA * sB * cG
let rB = sA * sG - cA * sB * cG
var compassHeading = -atan(rA / rB) + M_PI + deviceAngle
if rB > 0 { compassHeading += M_PI }

仍然需要对此进行测试,但现在它似乎可以工作。

【讨论】:

  • 如果您只有 CMMagneticField x、y、z(不是姿态)和重力怎么办?有没有办法从那里获得指南针方向?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 2014-09-25
  • 2014-04-13
  • 1970-01-01
相关资源
最近更新 更多