首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS中获取高程和磁场

如何在iOS中获取高程和磁场
EN

Stack Overflow用户
提问于 2013-03-07 22:26:21
回答 1查看 1.3K关注 0票数 1

我正在尝试获取iOS中的高程值和磁场值。我已经用谷歌搜索了很长一段时间,但我找不到任何可以正常工作的东西。

a)当我谈到仰角值时,我想要在我将手机顶部向上移动时获得角度。我附上了一张图片,让它更容易。我已经试过了,但是不起作用:

.h

代码语言:javascript
复制
#import <CoreMotion/CoreMotion.h>

@property (nonatomic, retain) CMMotionManager *motionManager;

.m

代码语言:javascript
复制
self.motionManager = [[CMMotionManager alloc] init];
[self.motionManager startMagnetometerUpdates];
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
[self.motionManager startDeviceMotionUpdatesToQueue:myQueue withHandler:^(CMDeviceMotion *motion, NSError *error){
    float degrees = (motion.attitude.roll*180) / M_PI;
    NSLog(@"Value of elevation is: %f", degrees);
}];

b)关于磁场,我想得到影响我的手机准确性的设备、磁铁、铁等的影响。*通过此解决方案(在一种NSTimer中),im获得的所有值都不正确:

代码语言:javascript
复制
NSLog(@"magnetometer data -> x: %f", self.motionManager.magnetometerData.magneticField.x);
NSLog(@"magnetometer data -> y: %f", self.motionManager.magnetometerData.magneticField.y);
NSLog(@"magnetometer data -> z: %f", self.motionManager.magnetometerData.magneticField.z);

float magneticField = sqrtf( powf(self.motionManager.magnetometerData.magneticField.x, 2) 
+ powf(self.motionManager.magnetometerData.magneticField.y, 2) + 
powf(self.motionManager.magnetometerData.magneticField.z, 2) );

NSLog(@"magneticField value: %@", magneticField.text);

有没有关于如何计算out....Thanks的想法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-08 20:05:41

最后,我得到了这两个问题的答案。这是我正在使用的代码。希望能对某些人有所帮助。

MagneticField:.h

代码语言:javascript
复制
@property (nonatomic, retain) CLLocationManager *locationManager;

.m

代码语言:javascript
复制
@synthesize locationManager;

- (void)viewDidLoad
{
[super viewDidLoad];

// Starting compass setup
if(locationManager == nil)
    locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;

//Start the compass updates.
if (CLLocationManager.headingAvailable) {
    [locationManager startUpdatingHeading];
    [locationManager startUpdatingLocation];
}
else {
    NSLog(@"No Heading Available: ");
    UIAlertView *noCompassAlert = [[UIAlertView alloc] initWithTitle:@"No Compass!" message:@"This device does not have the ability to measure magnetic fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [noCompassAlert show];
}
}

-(void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *)newHeading
{
NSLog(@"Magnetic value: %@", [[NSString stringWithFormat:@"%.1f", sqrt(newHeading.x*newHeading.x + newHeading.y*newHeading.y + newHeading.z*newHeading.z)] stringByAppendingFormat:@" µT"]);
}

标高:.h

代码语言:javascript
复制
@property (nonatomic, retain) CMMotionManager *motionManager;

.m

代码语言:javascript
复制
@synthesize motionManager;

- (void)viewDidLoad
{
[super viewDidLoad];
// Initializating MotionManager Object    
motionManager = [[CMMotionManager alloc] init];
motionManager.showsDeviceMovementDisplay = YES;
motionManager.deviceMotionUpdateInterval = 20.0/60.0;

// Getting Elevation Value
CMDeviceMotionHandler  motionHandler = ^ (CMDeviceMotion *motion, NSError *error) {
    CMAttitude *a = motionManager.deviceMotion.attitude;
    NSLog(@"Elevation value, %@:", [[NSString stringWithFormat:@"%.0f", a.pitch*180/M_PI] stringByAppendingFormat:@"˚"]);
};
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];
}

干杯。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15273708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档