This should be fairly easy to figure out I think…
I have a working mapview which drops a series of numbered pin markers onto the map. I’m calling the image for the pin from a plist however, when the pins drop they all have the same number that of the last pin. (instead of 1,2,3..)
The offending code is below, i just am not sure how to fix it… thanks.
NSString *path = [[NSBundle mainBundle] pathForResource:[route objectForKey:NAME_KEY] ofType:@"plist"];
NSMutableArray* array = [[NSMutableArray alloc] initWithContentsOfFile:path];
points = array;
for (NSDictionary *routeDict in points){
AllAnnotations *Annotation = [[AllAnnotations alloc] initWithDictionary:routeDict];
[mapView addAnnotation:Annotation];
[Annotation release];
}
for (NSDictionary *routeDict in points){
NSString *first = [routeDict objectForKey: POINT_KEY];
NSString *getNum = [routeDict objectForKey: COLOR_KEY];
NSString *again = [getNum stringByAppendingString:first];
NSString *imgValue = [again stringByAppendingString:@".png"];
annotationView.image = [UIImage imageNamed:imgValue];
}
annotationView.canShowCallout = YES;
AllAnnotations.m
------------------------------------------------------------------------------------------
- (id) initWithDictionary:(NSDictionary *) dict
{
self = [super init];
if (self != nil) {
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"name"];
self.subtitle = [dict objectForKey:@"subname"];
}
return self;
}
The second loop goes through all the points (annotations) and the current annotation view ends up with the image for the last point. The annotationView object is not changing inside that loop.
I assume that second loop and the last line are in the viewForAnnotation method.
Instead of looping through all points, you should get the routeDict from the current annotation object only and set the annotationView’s image once.
Assuming you’ve added routeDict as a property in the AllAnnotations class, you would do something like this:
Edit:
Based on the updated code in your question and comments, here are the changes you need to make.
In AllAnnotations.h, add an ivar to store the image file name for the annotation:
In AllAnnotations.m, add the synthesize for the new ivar and update the initWithDictionary and dealloc methods:
Alternatively, you could store the values of the POINT_KEY and COLOR_KEY objects in the annotation and generate the filename in the viewForAnnotation method.
By the way, “AllAnnotations” is not a good name for this class which represents a single annotation. Perhaps “RouteAnnotation” would be better.
Finally, the viewForAnnotation method should look like this: