How to keep MKMapView in iOS 6 North oriented always when switching mode from MKUserTrackingModeFollowWithHeading
Asked Answered
G

3

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?

Gelding answered 13/4, 2013 at 5:13 Comment(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 :/Clearway
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.Decoration
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?Gelding
This is literally an exact duplicate of #10964055Margalit
R
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.

Roveover answered 15/5, 2013 at 23:46 Comment(0)
M
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://mcmap.net/q/1172534/-rotate-mapview-using-compass-orientation/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.

Margalit answered 19/4, 2013 at 19:1 Comment(3)
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.Clearway
Hmm, I wonder if the issue is fixed in a later version of iOS? I was testing on iOS 6.1.Margalit
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 deviceClearway
C
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.

Clearway answered 21/4, 2013 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.