【问题标题】:CMMotionManager - getting heading using CMMotionManager exclusivelyCMMotionManager - 仅使用 CMMotionManager 获取航向
【发布时间】:2013-04-04 18:06:46
【问题描述】:

我已经设法使用 CLLocationManager 获得罗盘航向,但由于我的应用已经使用 CMMotionManager,我想只使用 CMMotionManager 来测量航向。

像所有 Apple 模糊文档一样,您必须猜测如何使用他们的 API。

我尝试用它来测量航向

[self.motionManager setMagnetometerUpdateInterval:1.0/30.0]; //30 Hz

[self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) {
    CMMagneticField compass = magnetometerData.magneticField;
    NSLog(@"x: %f  y:%f  z:%f", compass.x, compass.y, compass.z);
}];

问题是这个块只运行了几秒钟然后停止运行。

有没有专门使用 CMMotionManager 测量航向的例子?

谢谢。

【问题讨论】:

  • 你是否强烈引用 self.motionManager ?

标签: iphone ios ipad cmmotionmanager


【解决方案1】:

显然,最好的方法是使用 CoreLocation,直到 Apple 创建体面的文档。

【讨论】:

    【解决方案2】:

    如果没有实际尝试,我建议使用[NSOperationQueue mainQueue] 而不是 currentQueue。通常,您只在操作中调用 currentQueue 以获得对启动它的队列的引用。

    【讨论】:

      【解决方案3】:

      我建议您不要致电start...UpdatesToQueue:withHandler:。这需要使用后台线程,并且管理起来非常棘手。你可能只是在主线程上把所有东西都搞砸了。这是一种先进的技术,你不需要它。

      相反,只需start,然后使用 NSTimer 或类似方法重复轮询 CMMotionManager。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-18
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多