1

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);

理解する方法はありますか....ありがとう

4

1 に答える 1

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

乾杯。

于 2013-03-08T12:05:40.823 に答える