Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS MapKit camera resetting heading values between 6 and 354

I have an MKMapView and I am trying to rotate the map camera heading to follow the users heading. I don't want to do the auto-tracking because it forces a zoom level which is something I would like to have control over at the same time.

The thing is that I have found that the camera heading will revert to 0 if supplied with any value between 354 and 6 degrees. When I set the camera heading to 2 for example, it will constantly bounce between 2 and 0 until I turn the phone to a different heading.

My solution has been to just lock it to 0 when between this range but this isn't optimum. Does anyone have any information as to why this would happen? The method that gets called on heading changes looks like this:

- (void)setMapCameraPosition {
    //354,6
    if (self.heading <= 6 || self.heading >= 354) {
        NSLog(@"****nulling heading. self.heading: %d camera.heading: %f", self.heading, self.mapView.camera.heading);
        self.mapView.camera.heading = 0.0f;
    }
    else {
        NSLog(@"changing heading. self.heading: %d camera.heading: %f", self.heading, self.mapView.camera.heading);
        self.mapView.camera.heading = (double)self.heading;
    }
}

If I don't have the if statement in and just set the camera to my heading, then my output ends up being:

self.heading: 357 camera.heading: -0.000000
self.heading: 357 camera.heading: -0.000000
self.heading: 357 camera.heading: -0.000000

So it seams that if I give a value that is close to north, it will auto revert to north a split second afterwards - making the map jump for forever.

How can I set the heading to a degree close to, but not actually, north without the map removing my desired heading?

like image 911
shiznatix Avatar asked Oct 13 '14 12:10

shiznatix


1 Answers

At the moment this problem seems not to be fixable. I ran into this problem too and I am afraid you will need to use the technical support from Apple. Although I'm afraid the answer will be that this isn't possible at the moment, with the current SDK. Also, the Maps application has the same behaviour.

During my test I discovered the snapping happens for <7 degrees and >353 degrees. I used a slider to test the MKMapCamera heading function in an isolated app to make create a reproducible problem.

So you solution is the best for now. However I would snap not only to zero, but also 353 and 7 degrees.

float heading = self.heading;

if( roundf( heading ) >= round( 353.0f  ) && roundf( heading ) < round( 356.5f  ) ) {
    heading = 353.0f;
} else if( roundf( heading ) >= round( 356.5f  ) && roundf( heading ) <= round( 360.0f  ) )  {
    heading = 360.0f;
} else if( roundf( heading ) >= round( 0.0f  ) && roundf( heading ) < round( 3.5f  ) )  {
    heading = 0.0f;
} else if( roundf( heading ) >= round( 3.5f  ) && roundf( heading ) <= round( 7.0f  ) )  {
    heading = 7.0f;
}

[[[self mapview] camera] setHeading: heading];

But that is totally up to you.

like image 139
gjadmiraal Avatar answered Oct 14 '22 03:10

gjadmiraal