Question

Using the CLLocationManager object's didUpdateHeading event, how do I convert the resulting heading.x and heading.y values into degrees I can plot onto an image of a compass?

Was it helpful?

Solution

For headings degrees you can use magneticHeading and trueHeading properties instead of x and y.

trueHeading

The heading (measured in degrees) relative to true North. (read-only)

@property(readonly, nonatomic) CLLocationDirection trueHeading

Discussion

The value in this property represents the heading that points toward the geographic North Pole. The value in this property is always reported relative to the top of the device, regardless of the device’s physical or interface orientation. The value 0 represents true North, 90 represents due East, 180 represents due South, and so on. A negative value indicates that the heading could not be determined.

OTHER TIPS

This is how I rotated the uiimageview with the image of a compass. The North needle was originally pointing upwards in the image.

if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            NSLog(@"UIInterfaceOrientationLandscapeLeft");
            [self.compassImageViewIphone setTransform:CGAffineTransformMakeRotation((((newHeading.magneticHeading - 90) *3.14/180)*-1) )];

        }else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight){
            NSLog(@"UIInterfaceOrientationLandscapeRight");
            [self.compassImageViewIphone setTransform:CGAffineTransformMakeRotation((((newHeading.magneticHeading + 90) *3.14/180)*-1))];

        }else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            NSLog(@"UIInterfaceOrientationPortraitUpsideDown");
            [self.compassImageViewIphone setTransform:CGAffineTransformMakeRotation((((newHeading.magneticHeading + 180) *3.14/180)*-1) )];

        }else{
            NSLog(@"Portrait");
            [self.compassImageViewIphone setTransform:CGAffineTransformMakeRotation((newHeading.magneticHeading *3.14/180)*-1)];
        }

Try:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

   CLLocationDirection trueNorth = [newHeading trueHeading];

   CLLocationDirection magneticNorth = [newHeading magneticHeading];

}

CLLocationDirection is typedef double and so you get the true or magnetic heading in degrees.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top