I am working on a little project that shows 7 different types of annotations on the map. My annotations are taken from a url result in array and I parse it using JSON. I have lots of annotations and everything seems to look good once the map loads. After zooming in and zooming out, the the pin images changes for some reason to the wrong pin image (a specific image, no clue why).
I am sure I am missing something here...may you please help :) ?
Here's one part of my code, let me know if you need anymore of it:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier;
if(_mapView.tag==1){identifier = @"TurbulencePin";}
if(_mapView.tag==2){identifier = @"IcingPin";}
if(_mapView.tag==3){identifier = @"WindsPin";}
if(_mapView.tag==4){identifier = @"TemperaturePin";}
if(_mapView.tag==5){identifier = @"CloudsPin";}
if(_mapView.tag==6){identifier = @"VisibilityPin";}
if(_mapView.tag==7){identifier = @"MultiplePin";}
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[Annotation class]]) {
CustomAnnotationView* annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
annotationView = nil;
if (annotationView == nil) {
annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",identifier]];
annotationView.image = img;
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
Update:
Based upon feedback of others, I've modified the code for the image setting to be as follows:
Annotation *myCustomAnn = (Annotation *)annotation;
NSString *imgName = myCustomAnn.imageName;
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%@Pin.png",imgName]];
annotationView.image = img;
return annotationView;
Plus, I removed the annotationView = nil;
However, I cannot set the image name in the annotation.m as a hardcoded value because I need to display a different pin image for each annotation. I'm sure that there is an explanation but the only value that I can get from the annotation.m under the mapView:viewForAnnotation: is the annotation coordinates (myCustomAnn.coordinate.latitude
and myCustomAnn.coordinate.longitude)
, I have no clue how to get other properties from the annotation.m
The other properties, such as title, imgname etc comes back as null