Search code examples
objective-cios6mkmapviewmapkitcgaffinetransform

Rotation MKMapView without Upside-Down Labels


UPDATE

Apple fixed this is iOS 7. More info after public launch.


So, I have this bit of code:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    CGAffineTransform transform = CGAffineTransformMakeRotation(degToRad([newHeading trueHeading]));
    [mapView setTransform: transform];
}

Which beautifully renders directionally rotated map, like so:

enter image description here

Problem is, the labels are all upside down. Now, I'm pretty sure one can work around this, because I believe Apple touted the ability to rotate labels in an iOS 6 keynote. I'm just not seeing how.

For comparison, the effect I'd like, as implemented in the stock Maps app:

enter image description here

TL;DR

How do I rotate the map, but keep the labels right-side up? Or at least rotate the labels independently of the map?


Solution

  • Applying a transformation onto the map is not the correct way to apply user heading rotations. Metaphorically speaking you're rotating the entire canvas instead of the elements on the canvas.

    What you want to do is to call: [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES]; (for instance in your viewDid/WillAppear method or some IBAction trigger method.

    You can read more about it here: http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/setUserTrackingMode:animated:

    By calling above method you tell the map view to start updating the heading (rotation). Label rotations will then adjust automagically.