6

I am switching between the three different map orientation modes using MKUserTrackingModeNone, MKUserTrackingModeFollow, MKUserTrackingModeFollowWithHeading and this works.

However I have a problem with the orientation of the map not resetting to north-facing orientation (north on the map being at the top of the screen) when switching from MKUserTrackingModeFollowWithHeading to MKUserTrackingModeNone.

On the built-in maps app on the iphone/ipad, the flow is like this:

When you start the app it is in mode MKUserTrackingModeNone and is north-oriented When you toggle the orientation mode it changes to MKUserTrackingModeFollow, and the orientation is still north.

When you switch again, it changes to MKUserTrackingModeFollowWithHeading, and the map rotates according to the direction you are facing/pointing the iPhone.

When you switch orientation again, it goes back to MKUserTrackingModeNone, and the map nicely rotates back to being north-oriented.

I would like my app to behave in the same way in regards to orientation when switching mode, but when I do as in step 4 above and switch from MKUserTrackingModeFollowWithHeading to MKUserTrackingModeNone, the orientation stays as it was just before making the orientation switch instead of rotating back to north orientation.

I am making the orientation switch with the standard MKUserTrackingBarButtonItem control placed in a toolbar.

Anyone please help me to fix this issue?

Rajendra_Prasad
  • 1,300
  • 4
  • 18
  • 36
real hat
  • 61
  • 1
  • 4
  • Sorry I can't help you on that except to say that in my app it does what you want and I haven't done anything beyond that same standard button :/ – Craig Apr 13 '13 at 07:25
  • 1
    This may or may not help since my experience is with iOS 5 - make sure you're letting the built-in functionality (of the map and the `MKUserTrackingBarButtonItem`) take care of the orientation mode. Make sure you do not have any code trying to set the orientation mode on the map. – Jesse Apr 15 '13 at 12:14
  • I am not setting any orientation mode. You mean to say , we dont need to set the supportedInterfaceOrientations in the controller we add the map? or is there any other orientation mode which can be set on MKMapView? – real hat Apr 16 '13 at 11:55
  • This is literally an exact duplicate of http://stackoverflow.com/questions/10963555/how-to-rotate-ios-mkmapview-so-that-it-north-oriented-when-switching-tracking-mo – Aaron Brager Apr 19 '13 at 18:55

3 Answers3

1

I'm also running into this issue on an iPhone 5 running iOS 6.1.4. I used this simple but ugly quick fix to force the map view to rotate back to North up:

-(void)someMethod
{
    // Currently in MKUserTrackingModeFollowWithHeading mode

    // Set tracking mode back to MKUserTrackingModeFollow
    [_mapView setUserTrackingMode:MKUserTrackingModeFollow];

    // After a short delay, set mode to MKUserTrackingModeNone
    [self performSelector:@selector(mapViewTrackingModeNone)
               withObject:nil
               afterDelay:0.2];

}

- (void)mapViewTrackingModeNone
{
    [_mapView setUserTrackingMode:MKUserTrackingModeNone];

    // Bang! The map rotates back to North-Up
}

There's probably a much better way to do this, but I haven't found it yet.

Dylan
  • 545
  • 4
  • 15
0

In iOS 5, it will switch north for you, but in iOS 6 it does not. You can file an enhancement request on bugreport.apple.com.

You could implement the delegate callback function and apply a rotation in the delegate callback...

- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    if (mode == MKUserTrackingModeNone)
        [self rotateTheMapView];
}

- (void)rotateTheMapView {
    // See https://stackoverflow.com/q/1245461/1445366
}

(See Rotate MapView using Compass orientation for rotation instructions & code.)

The problem is that Apple doesn't expose the exact current rotation using its internal logic, so any calculations from the current heading provided to you by iOS may be slightly off, causing your map to be slightly off-north when you're done.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • I've tested my app on an iPhone 4S and and iPad 3 running iOS 6.1.3 and both return to north when I switch the tracking mode back to none by cycling through the states of the MKUserTrackingBarButtonItem. No other code is required. – Craig Apr 21 '13 at 18:55
  • Hmm, I wonder if the issue is fixed in a later version of iOS? I was testing on iOS 6.1. – Aaron Brager Apr 22 '13 at 18:00
  • It's unlikely since it worked when I had my device on iOS 5 too. The best way to find out is to update your device – Craig Apr 22 '13 at 20:18
0

You should put together a small test app, that only has a mapview in it and a MKUserTrackingBarButtonItem and link the two together. If it should do as you described and not require a single line of code. All the connections can be done in IB. Once you see that working you can return to your code and repeat the process.

Craig
  • 8,093
  • 8
  • 42
  • 74