bentroc
bentroc

Reputation: 61

MKAnnotationView rotation

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.

Upvotes: 2

Views: 1917

Answers (1)

bentroc
bentroc

Reputation: 61

To answer my question, the problem lied in the fact I was calling the delegate method MKAnnotationView* planeView = [self mapView:mapView viewForAnnotation:planeAnnotation];instead of calling the instance method MKAnnotationView* planeView = [mapView viewForAnnotation:planeAnnotation]; when transforming the MKAnnotationView.

Upvotes: 4

Related Questions