After building a simple application which allows you to drop annotations onto a map, I decided to take it one step further and allow for the switching on and off of the compass.
In proper maps application, there is normally a picture of a compass or similar, which is in a fixed position when the compass is off. It is normally animated when the compass is switched on.
When the user moves the map with their finger, the heading tracking is automatically turned off.
Putting in an animated compass is beyond me at this stage, but I decided to do the same thing with a switch (see screenshot 1).
The user can flip the switch to on to active heading tracking. The following code does this:
The compass then switches on successfully. Flipping the switch to off does the opposite.
If the compass can switch off when the user moves the map, the switch needs to also be set to "off" when this happens otherwise they will end up out of sync. I added the following:
The problem is that this only seems to work once; the first time you flick the switch on and then off, it works. However, when the switch is flicked to "on" on subsequent occasions it immediately turns off. It is as if the animation to swivel the map when the compass is switched on is treated as a regionDidChangeAnimated event, and so switches it off again.
Interestingly, when the animation argument is set to "NO" in the setUserTrackingMode method, this strange behaviour doesn't happen.
On the basis it works in the proper maps application, does anyone have any idea why this is happening?
Thank you
In proper maps application, there is normally a picture of a compass or similar, which is in a fixed position when the compass is off. It is normally animated when the compass is switched on.
When the user moves the map with their finger, the heading tracking is automatically turned off.
Putting in an animated compass is beyond me at this stage, but I decided to do the same thing with a switch (see screenshot 1).
The user can flip the switch to on to active heading tracking. The following code does this:
Code:
- (IBAction) toggleHeadingStatus : (id) sender {
if ([headingSwitch isOn]==YES) {
[worldView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
} else {
[worldView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
}
The compass then switches on successfully. Flipping the switch to off does the opposite.
If the compass can switch off when the user moves the map, the switch needs to also be set to "off" when this happens otherwise they will end up out of sync. I added the following:
Code:
- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if ([worldView userTrackingMode]==MKUserTrackingModeFollowWithHeading) {
[headingSwitch setOn:YES animated:YES];
} else {
[headingSwitch setOn:NO animated:YES];
}
}
The problem is that this only seems to work once; the first time you flick the switch on and then off, it works. However, when the switch is flicked to "on" on subsequent occasions it immediately turns off. It is as if the animation to swivel the map when the compass is switched on is treated as a regionDidChangeAnimated event, and so switches it off again.
Interestingly, when the animation argument is set to "NO" in the setUserTrackingMode method, this strange behaviour doesn't happen.
On the basis it works in the proper maps application, does anyone have any idea why this is happening?
Thank you