我试图在iOS中获得高程值和磁场值。我已经在谷歌上搜索了很长时间关于这些主题,但我找不到任何工作正常。
a)当我谈论仰角值时,我想要得到以度为单位的角度,当我移动手机的顶部时。我附上了一个图像,使它更容易。我一直在尝试这样做,但它不工作:
. h
#import <CoreMotion/CoreMotion.h>
@property (nonatomic, retain) CMMotionManager *motionManager;
m
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中)获取的所有值都不正确:
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);
有什么想法吗....谢谢
最后,我得到了两个问题的答案。这是我使用它的代码。希望对大家有所帮助。
磁场:. h
@property (nonatomic, retain) CLLocationManager *locationManager;
m
@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
@property (nonatomic, retain) CMMotionManager *motionManager;
m
@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];
}
欢呼。