Silver Challenge Solution: Heading


Added this code to initWithNibName:bundle method:

if ([CLLocationManager headingAvailable]) { [locationManager startUpdatingHeading]; } // Used the +(BOOL)headingAvailable class method to test if the current iOS device supports heading data (has a compass). // If true, then message the locationManager object to startUpdatingHeading. // As referenced in the CLLocationManager docs, if heading data is not supported on the iOS device, calling the // startUpdatingHeading method will have no effect and events won't get sent to your delegate.
Implemented this method from the CLLocationManagerDelegate Protocol:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { NSLog(@"%@", newHeading); } // Tell the delegate that the location manager recieved updated heading information.
Here’s a few lines from the console output after moving my phone around to test it:

2012-04-10 15:17:01.857 Whereami[6770:707] magneticHeading 275.29 trueHeading 271.97 accuracy 10.00 x +28.346 y -10.426 z -44.273 @ 2012-04-10 20:17:01 +0000 2012-04-10 15:17:24.685 Whereami[6770:707] magneticHeading 264.83 trueHeading 261.51 accuracy 10.00 x +18.770 y -15.456 z -46.775 @ 2012-04-10 20:17:24 +0000 2012-04-10 15:17:24.710 Whereami[6770:707] magneticHeading 260.80 trueHeading 257.48 accuracy 10.00 x +16.771 y -16.490 z -46.989 @ 2012-04-10 20:17:24 +0000 2012-04-10 15:17:24.736 Whereami[6770:707] magneticHeading 258.80 trueHeading 255.48 accuracy 10.00 x +15.462 y -18.350 z -47.347 @ 2012-04-10 20:17:24 +0000


Hi bryanluby,

Thank you for sharing the solution codes. I’m wondering if I need to register a developer program in order to test retrieving heading information.



@tomauhk I tried simulating movement on the iOS simulator, under Debug > Location > City run, but no heading data will get sent.

I also tried commenting out the code that tests for a hardware compass, but got no result (which makes sense since no compass data events will get sent to the delegate):

/*if ([CLLocationManager headingAvailable]) { [locationManager startUpdatingHeading]; }*/ [locationManager startUpdatingHeading];
A quote from the CLLocationManager Class Reference:[quote]Some location services require the presence of specific hardware on the given device. For example, heading information is available only for devices that contain a hardware compass.[/quote]
So it sounds like you need a registered developer program account and a device that has a compass to see the heading data.