Rotate user location annotation header

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!

+4
source share
1 answer

Even if it is likely that you have already fixed your 2. problem, I still want to share a solution that helped me with a similar problem.

I ran into the same problem: my custom rotation of the location arrow always returned north when the user was scrolling or noticing the map. I found a fix here: fooobar.com/questions/1455923 / ...

according to the author, it is, but in ios6 and can be fixed by adding

 if(is6orMore) { [annView setTransform:CGAffineTransformMakeRotation(.001)]; //iOS6 BUG WORKAROUND !!!!!!! } 

for mapView: viewForAnnotation.

I also set the in - mapView transformation: regionDidChangeAnimated: again. Hope this makes someone else as happy as me :)

+2
source

All Articles