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?
Asked
Active
Viewed 1.7k times
1 Answers
32
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);

Nur Iman Izam
- 1,613
- 1
- 15
- 12

Keller
- 17,051
- 8
- 55
- 72
-
Yes, I think you're right about the native compass UIView. I'll wait a little to see if anyone else disagrees. Certainly yes, didUpdateHeading is the way to update whatever image i choose to draw. Thanks so much. – theDuncs Feb 13 '12 at 18:07
-
1Keller - since nobody else has contradicted you, I'll take your answer as the correct one. Thanks mate. – theDuncs Feb 20 '12 at 15:53
-
No problem. To create a compass view that points north, simply create a "compass needle" graphic and rotate it via it's transform. See my Edit. – Keller Feb 21 '12 at 01:25
-
3Here is a full tutorial on how to implement this if anyone was wondering: http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone/ – Albert Renshaw Dec 19 '12 at 06:04
-
@AlbertRenshaw http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone/ link not working. Please help me to create compass in IOS objective-c – Dipanki Jadav Nov 17 '16 at 12:32
-
@DipankiJadav We are in luck! Archive.org has a copy of the site from before it went down! https://web.archive.org/web/20150421132328/http://blog.objectgraph.com/index.php/2012/01/10/how-to-create-a-compass-in-iphone – Albert Renshaw Nov 17 '16 at 17:52