I'm trying to change the user annotation in my application so that it displays a regular blue dot, but with a triangle exiting from it, to show which direction the user is in (I would rather rotate the user annotation than the entire map, which MKUserTrackingModeFollowWithHeading does). I have a rudimentary version, but it has a bit strange behavior.
First, some code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { _userLocationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"userLocationIdentifier"]; //use a custom image for the user annotation _userLocationView.image = [UIImage imageNamed:@"userLocationCompass.png"]; return _userLocationView; } else { return nil; } } -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { //rotate user location annotation based on heading from location manager. if (!_locatorButton.hidden) { CLLocationDirection direction = newHeading.magneticHeading; CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(direction)); _userLocationView.transform = transform; } } -(void)GPSButtonPressed:(id)sender { if (self.GPSEnabled) { //if GPS is already on, disable it. _mapview.showsUserLocation = NO; [_mapview removeAnnotation:_mapview.userLocation]; self.GPSEnabled = NO; [_locationManager stopUpdatingHeading]; } else { //enable GPS. _mapview.showsUserLocation = YES; self.GPSEnabled = YES; if ([CLLocationManager headingAvailable]) { [_locationManager startUpdatingHeading]; } } }
The user location annotation image displays fine and rotates depending on the title, but here's the fun behavior:
1- Annotations do not follow my location - it remains in only one place, but rotates with the correct title. If I turn off the GPS using the βGPS buttonβ and then turn it on, the annotation will appear in the right place, but still will not follow when I go.
2- If I scroll the map, the annotation will return to the north, and then quickly turn to the correct course, causing an annoying flickering effect.
3- If I turn off the GPS and delete the user's location, the annotation will be deleted as intended, but if I then scroll through the map, the annotation will appear on the screen and will not remain hidden.
What am I doing wrong? Thanks for the helpful hints!
source share