Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use native iOS compass within an app

Is it possible to use the native compass that iOS has within my own application? Or do I need to draw and animate my own compass?

like image 213
theDuncs Avatar asked Feb 13 '12 17:02

theDuncs


1 Answers

There is no native compass UIView. In order to use the magnetometer, you'll have to use CoreLocation and the following delegate method:

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

to rotate a UIView to point North (bearingView is a UIImageView):

float heading = newHeading.magneticHeading; //in degrees
float headingDegrees = (heading*M_PI/180); //assuming needle points to top of iphone. convert to radians
self.bearingView.transform = CGAffineTransformMakeRotation(headingDegrees);
like image 95
Keller Avatar answered Nov 04 '22 18:11

Keller