1

from Apple's Documentation :

@property(readonly, nonatomic) CMMagneticField magneticField

Returns the magnetic field measured by the magnetometer. (read-only)

The value of this property is the total magnetic field observed by the device which is equal to the Earth’s geomagnetic field plus bias introduced from the device itself and its surroundings.

@property(readonly, nonatomic) CMCalibratedMagneticField magneticField

The CMCalibratedMagneticField returned by this property gives you the total magnetic field in the device’s vicinity without device bias. Unlike the magneticField property of the CMMagnetometer class, these values reflect the earth’s magnetic field plus surrounding fields, minus device bias.

Which of the two do I need to use if I want to get the device's deviation from the North in degrees? That is,the value a digital compass on the iPhone would display, based on where the device is facing (e.g. 90, 180, 193, etc..)

I know that none of the above return degrees in integer format. The documentation is not clear as to what exactly they return.. I think it's MicroTeslas..

But if they do return a MicroTesla value how do I convert this value into an integer representing degrees? Do I have to multiply that MicroTesla value with some constant or something of that nature?

Thanks for any help!

Kev
  • 118,037
  • 53
  • 300
  • 385
  • The magnetic field data contains x, y, and z components. You'd presumably have to do the trig necessary to convert that to a heading. Of course, keep in mind that the Earth's magnetic field is not "perfect", so especially near the poles you'd need to apply corrections to get the "true" heading. – Hot Licks Jul 06 '12 at 15:03

2 Answers2

4

As a side note, if you want to get the magnetic declination for your current position you can simply subtract the magnetic heading from the true heading. The number is useful for converting magnetic compass bearings to true north bearings.

CLLocationDirection magneticDeclination = newHeading.trueHeading - newHeading.magneticHeading;
Jeremy Whitcher
  • 611
  • 7
  • 10
1

You need to measure the device heading which you get from CLLocationManager. Heres how you start getting the heading updates (first import core Location framework) -

if([CLLocationManager headingAvailable]) //check if device has magnetometer
    [locationManager startUpdatingHeading];

after this you get updates in this delegate -

- (void)locationManager:(CLLocationManager*)manager 
       didUpdateHeading:(CLHeading*)newHeading
{
    // If the accuracy is valid, process the event.
    if (newHeading.headingAccuracy > 0)
        CLLocationDirection theHeading = newHeading.magneticHeading;

    return;
}

so CLLocationDirection represents a direction that is measured in degrees relative to true north.

Direction values are measured in degrees starting at due north and continue clockwise around the compass. Thus, north is 0 degrees, east is 90 degrees, south is 180 degrees, and so on. A negative value indicates an invalid direction.

hopt his helps...

UPDATE: do check out these links - CLLocationManager and degrees of heading in iPhone and iPhone 3.0 Compass: how to get a heading? and apple reference.

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • just a quick maybe stupid one : is CLLocation Manager using the GPS or the magnetometer because I want to rely solely on the device's magnetometer.. thanks :) –  Jul 06 '12 at 15:09
  • since x,y,z are measured based on geomagnetic data I am guessing the heading too is based on that. But since you are interested in knowing the device heading relative to north why does it matter to you? – Srikar Appalaraju Jul 06 '12 at 15:15
  • because if it relied on GPS for that and GPS wasn't available I wouldn't get anything whereas the device's magnetometer s always available.. But you're right it doesn't make sense for it to be using the GPS for such a reading.. –  Jul 06 '12 at 15:18
  • dont worry about that. leave those details for iOS and apple to handle. They have give you a nice API to get device heading use it. – Srikar Appalaraju Jul 06 '12 at 16:19
  • So if I understand correctly, when using the heading property the you suggested, I don't have to worry about how these values are given to me. The values might come from GPS or the magnetometer of the device or from a combination of both, but I will always get a value regardless of whether GPS is available or not (based on the premise that my device has a magnetometer that is..). Is that correct? –  Jul 06 '12 at 16:24
  • yes thats what I am made to understand when reading the Apple documentation for the same. Sweet isnt it :) But for that matter thats why API's are built, to abstract complexity. – Srikar Appalaraju Jul 06 '12 at 16:26
  • Actually I read the documentation again (http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/cl/CLLocationManager) >> –  Jul 07 '12 at 19:50
  • >> and they state rather explicitly that "Important In addition to hardware not being available, the user has the option of denying an application’s access to location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. You can also check the application’s explicit authorization status using the authorizationStatus method." –  Jul 07 '12 at 19:50
  • 1
    >> what the above means, if I understand correctly, is that enabling the GPS is necessary for this to work, whereas I only want to use the magnetometer and bypass the trouble of asking the suer whether he wishes to enable location services, consume battery, etc.. etc.. So I don't think CLLocation Manager is the answer to my question :( –  Jul 07 '12 at 19:52
  • 1
    In case anyone's interested, this question was eventually answered over here : http://stackoverflow.com/questions/11383968/which-ios-class-code-returns-the-magnetic-north/11384054#11384054 –  Jul 08 '12 at 16:25