I have a moving annotation on a MKMapView showing the current user location. I set up my own image for that annotation and I want to rotate it so that you can see the heading.
In the viewForAnnotation delegate, I have:
static NSString *planeIdentifier = @"Plane";
static NSString *stationIdentifier = @"Station";
NSString *identifier;
if([[annotation title] rangeOfString:@"Plane" options:NSLiteralSearch].location == NSNotFound) {
identifier = stationIdentifier;
}
else {
identifier = planeIdentifier;
}
MKAnnotationView *annotationView = (MKAnnotationView *) [aMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:@"airPlane.png"];
return annotationView;
The code for calling the instance method is the following:
[planeAnnotation setCoordinate:planeCoordinate];
MKAnnotationView* planeView = [self mapView:mapView viewForAnnotation:planeAnnotation];
[planeView setTransform:CGAffineTransformMakeRotation([[[self modele] udpData] getValueFor:HDING_TRUE item:DYNAMICS]*(M_PI/180))];
The problem is the heading is never updated i.e. the image never rotates.
Any help much appreciated!
EDIT: I have tried setting a custom identifier (the title of the annotation) for each MKAnnotationView I have on the mapView (a few hundred), but I noticed the annotationView is always nil, meaning it doesn't actually dequeue anything from it. I think this is the reason why it doesn't rotate, since i'm not rotating the same MKAnnotationView I am displaying on the map.
heading
change? Are all the hundreds of annotations supposed to rotate or just one? Will hundreds be visible all at once? See this related question the answer to which suggests using theviewForAnnotation
instance method to retrieve the current view and you can try transforming it in real time instead of removing and adding again. – user467105heading
variable changed 20 times per second, it is a value received on a UDP port. Only one annotation is supposed to rotate, the one showing the movement of the user on the map. All the others are fixed stations. They are not all visible at once, you need to scroll the map. I have tried using the instance method to transform the view, but nothing changed. I think the problem may come from the fact my MKAnnotationView are not properly reused, causing an other (now hidden) view to be edited. – bentrocannotationView.annotation = annotation;
is never called, showing the MKAnnotationView is never reused. – bentroc