2013-03-07 80 views
1

我試圖獲取iOS中的海拔高度值和磁場值。我一直在搜索這些主題,但我無法找到任何可以正常工作的東西。 a)當我在談論仰角值時,我想在將我的手機頂部向上移動時以度爲單位獲得角度。我附上了一張圖片,以方便使用。 我一直在嘗試這樣的,但它不工作:如何獲取iOS中的海拔高度和磁場值

.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); 
}]; 

二)關於磁場,我想要得到的影響設備,磁鐵,鐵等,影響我的手機的準確性。 *所有值即時得到不正確的這個解決方案(一種的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); 

任何想法如何找出....謝謝

回答

1

最後,我得到的答案對這兩個問題。這是我使用它的代碼。希望它可以幫助某人。

磁感應強度: .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]; 
} 

乾杯。

+0

嗨rubrin 感謝您發佈您的答案尋找海拔。 我只想知道在哪種格式下我可以找到標高值? – 2017-07-31 05:24:37