问题
I'm trying to change the User Annotation in my app so that it shows the usual blue dot, but with a triangle coming off of it to show which direction the user is facing (I'd rather rotate the user annotation than the entire map, which is what MKUserTrackingModeFollowWithHeading does). I've got a rudimentary version working, but it has some weird 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 shows up fine, and rotates based on the heading, but here are the funny behaviors:
1- The annotation does not follow my location-- it only stays in one place, but rotates with the correct heading. If I turn off the GPS with the "GPS Button", then turn it back on, the annotation shows up in the correct place, but still won't follow as I walk.
2- If I scroll the map, the annotation pops back to due north, then quickly rotates to the correct heading, causing an annoying flickering effect.
3- If I turn off the GPS and remove the user location, the annotation is removed as intended, but if I then scroll the map, the annotation pops back to the screen rather than staying hidden.
What am I doing wrong? Thanks for any helpful hints!
回答1:
Even if it is likely that you already fixed your 2. problem I still want to share the solution, which helped me with a similar problem.
I came across same problem: my custom user location arrow rotation was always popping back to north when the user scrolled or panned the map. I found the fix here: https://stackoverflow.com/a/12753320/2030629
according to the author it is a but in ios6 and can be fixed by adding
if(is6orMore) {
[annView setTransform:CGAffineTransformMakeRotation(.001)]; //iOS6 BUG WORKAROUND !!!!!!!
}
to mapView:viewForAnnotation.
I also set the transform in - mapView:regionDidChangeAnimated: again. Hope this will make someone else as happy as it made me :)
来源:https://stackoverflow.com/questions/14158812/turning-user-location-annotation-with-heading