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
tag
property for this. (As an aside, youridentifier
doesn't need to be static if you're setting it programmatically.) But the only thing that I could imagine would cause the pin images to change is if thetag
is changing somehow. I'd suggest logging that and see what's going on (but, even better, abandontag
and use a custom property for your annotation). – Pecoraro_mapView
(which is some ivar) but rather usemapView
, the parameter to this method. – PecoraroimageName
. Then in your code that creates the annotation (your view controller?) after creating the annotation, that's where you set theimageName
property for your annotation. Regarding your other properties being null, the question is whether you set those properties in your VC where you did thealloc
/init
of the annotation object, but before you doaddAnnotation
call. – Pecorarocoordinates
you're parsing physically represent; and (b) what these seven annotation view images represent. Clearly we're all assuming that the annotation view's image is a function of the type of annotation. Is that not the case? – PecoraroaddAnnotation
should be the last step. Or alternatively, sometimes I'll add the annotations to aNSMutableArray
, tweak them to my heart's content, and then, at the very end, I'll useaddAnnotations
(note thes
), passing it my array. – Pecoraro