I am working on an old application and need to make changes in Mapview. Previously we need to show multiple annotations on mapview with same images on each pin but now we have to show different images on annotation view pins to display address. I am using the following code to display annotation pins but it always shows the same image on annotation pins.
Here is my code:
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
NSLog(@"Eventtype Array is %@",eventTypeArray);
MKAnnotationView * pinView = nil;
if(annotation != _mapvw.userLocation)
{
static NSString * defaultPinID = @"pinId";
pinView = (MKAnnotationView *)[_mapvw dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
}
for ( int i=0; i<[eventTypeArray count]; i++)
{
eventTypeStr = [NSString stringWithFormat:@"%@",
[eventTypeArray objectAtIndex:i]];
NSLog(@"Event Type is %@",eventTypeStr);
if ([eventTypeStr isEqualToString:@"0"])
{
NSLog(@"Eventtype Array is %@",eventTypeStr);
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"smiley.png"];
}
else if ([eventTypeStr isEqualToString:@"1"])
{
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"dollar1.png"];
}
else if ([eventTypeStr isEqualToString:@"2"])
{
NSLog(@"Event Type is %@",eventTypeStr);
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"donation.png"];
}
}
}
return pinView;
}
if else
. P.S. There are many other many other improvements possible in this code (like usingswitch
case instead ofif else
, enum / constant for your event identifier wtc.). Please consider a thorough review, and refactoring. – Swapnil Luktuke