Is it possible to have an embedded MKMapView
rotate to always face the direction the iPhone is facing? Basically I want to mimic the Map app rotation feature on my own app.
I see that the iPhone SDK does not expose the functionality. However, I wonder if it would work to rotate the entire view using CGAffineTransformMakeRotate
. Would it affect tapping and zooming? Is there a better way?
I can confirm that it works fine. Here's the code that I'm using:
mapView
is my MKMapView instanceTo rotate the mapView but not the annotations you could use the following code to compensate for the maps rotation.
Another sollution is using a new method that has been added in iOS 5 to MKMapView.
Take a look at: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
if you use a navigationVontroller try this:
If you look in the 3.0 iPhone Application Programming Guide under
Device Support
you'll find information on the magnetometer (aka compass). Once you start getting heading information (through thedidUpdateHeading
method) you should be able to get the compass data then use that to calculate the proper rotation transform value to apply to the map view.Not sure if it handles zooming. In the standard map application I've noticed that compass heading tracking stops as soon as you start pinch-zooming.
Also, keep in mind that location directions are in degrees whereas transform rotation angles are in radians.
Simple solution in swift 3.0. Make sure to put the line in
mapViewDidFinishLoadingMap
or it will ignore itIf you don't want the map to center on the user location you might do something like this