I'm implementing a MKMapView
in an app, and have implemented the -mapView: viewForAnnotation:
method in order to show dynamically placed pins with callouts on the map. The annotations on the map I'm interested in are defined in a separate class called Annotation
that implements MKAnnotation
. In the -mapView: viewForAnnotation:
method, I need to get the value of the variable which is fed in through the method's annotation
variable, which will be an instance of Annotation
.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[Annotation class]]) {
static NSString *identifier = @"RemoteLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
//HERE, I NEED TO GET THE VALUE OF annotation's VARIABLE IN ORDER TO ASSIGN THE ANNOTATIONVIEW IMAGE
//annotationView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:annotation.senderPhotoURL]]];
return annotationView;
}
return nil;
}
The problem is that when I try to get the value of the variable from annotation
, which is an instance of Annotation
, the variable isn't accessible. What do I need to do to inform the program that annotation
is an instance of Annotation
, so that the variable is available to read?
Although you have proved that annotation
is an Annotation
underneath you need to cast it to an actual instance of Annotation, like this:
Annotation* yourAnnotation = (Annotation *)annotation;
Then you can use yourAnnotation
as you'd expect.