1

我正在尝试使用新 iPhone 中的气压计传感器在设备上显示垂直速度。我最终在我的“运动”模型中得到了以下代码,它确实执行以提供或多或少适当的值。

但是,我在执行时更新我的​​视图方面遇到了问题startRelativeAltitudeUpdatesToQueue。的值userRelativeAltitude已更新,但该函数从不返回任何内容(可能是因为它正在startRelativeAltitudeUpdatesTo Queue像循环一样执行工作。?)。恐怕整个声明已经在我头顶结束了。

我开始拥有更像例如的功能startAccelerometerUpdates。有没有更好的方法从气压计收集数据?

另外,您如何检查设备是否有可用的气压计传感器?

    - (float) pullRelativeAltitude {

self._altitudeManager = [[CMAltimeter alloc] init];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[self._altitudeManager startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^ {

        _userRelativeAltitude = altitudeData.relativeAltitude.floatValue;
        NSLog(@"%f", altitudeData.relativeAltitude.floatValue);

    });
}];

NSLog(@"This is relative altitude: %f", _userRelativeAltitude is );

return _userRelativeAltitude;

}

建议将不胜感激!

4

3 回答 3

1

在 Swift 中,以下代码将检查高度是否可用:

if CMAltimeter.isRelativeAltitudeAvailable() {
  // your code
}

但是,我也无法获得任何气压计读数。

于 2014-09-20T19:54:29.040 回答
1

不知道你是否明白了,但这对我有用。
要添加的一件事,您可能已经想通了,您不能在模拟器中。您需要 iPhone 6 或 6+,并且该应用程序必须在手机上运行 :)

- (void)viewDidLoad {
    [super viewDidLoad];

    BOOL isReady = [CMAltimeter isRelativeAltitudeAvailable];
    if( isReady ) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
    alt = [[CMAltimeter alloc] init];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [alt startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^ {
            NSLog(@"%f", altitudeData.relativeAltitude.floatValue);

        });
    }];
}
于 2014-09-21T03:53:03.460 回答
1

我得到了它的工作,但我必须在视图控制器(而不是模型)中实现代码,以用新的读数更新 UI。对于其他感兴趣的人,我的最终编码最终看起来像这样:

if ([CMAltimeter isRelativeAltitudeAvailable]) {
    self._altitudeManager = [[CMAltimeter alloc] init];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [self._altitudeManager startRelativeAltitudeUpdatesToQueue:queue withHandler:^(CMAltitudeData *altitudeData, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^ {

            //provide relative altitude
            float relativeAltitude;
            relativeAltitude = altitudeData.relativeAltitude.floatValue;
            self.userRelativeAltitude.text = [NSString stringWithFormat:@"%.0f m", relativeAltitude];

        });
    }];

} else {
    NSLog(@"barometer not available");
    _userVario.text = @"no barometer available";
}

感谢您的回答!

于 2014-09-25T06:43:12.467 回答