View map annotations with different contact colors

I have an array with over 200 objects, and I'm trying to loop through each of them.

Each object will have a yes / no field, and I want to display a different color marker, depending on this yes / no value.

From what I see, it happens when my loop goes through each object first, and then the whole annotation is added to the end for each object.

Since I am checking inside my loop through an array with a value of yes no, when the whole annotation is added to my map, it will use the value yes / no from the last object in the array when it goes to the graph for everything.

How to do this so that the marker is different depending on the yes / no value for each individual element?

My code

for (i = 0; i < [appDelegate.itemArray count]; i++) {
        item_details *tempObj = [appDelegate.itemArray objectAtIndex:i];
        location.latitude = [tempObj.lat floatValue];
        location.longitude = [tempObj.lon floatValue];
        current_yesno = tempObj.yesno;
        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]initWithTitle:tempObj.name andCoordinate:location];
        [self.mapView addAnnotation:newAnnotation];
        [newAnnotation release];            
            } 

with my annotation code as follows

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if(current_yesno == YES){
    annView.pinColor = MKPinAnnotationColorGreen;
}
else
{
    annView.pinColor = MKPinAnnotationColorRed;
}
    annView.animatesDrop=NO;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;

}

current_yesno .h .

+5
2

viewForAnnotation , addAnnotation, , ( - ).

, , ivar .

yesno MapViewAnnotation, viewForAnnotation annotation (.. , ).

:

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.yesno = tempObj.yesno;  // <-- set property in annotation
[self.mapView addAnnotation:newAnnotation];

viewForAnnotation:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if (![annotation isKindOfClass:[MapViewAnnotation class]])
    {
        // Return nil (default view) if annotation is 
        // anything but your custom class.
        return nil;
    }

    static NSString *reuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];        
        annView.animatesDrop = NO;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
    }
    else
    {
        annView.annotation = annotation;
    }

    MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation;
    if (mvAnn.yesno)
    {
        annView.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {
        annView.pinColor = MKPinAnnotationColorRed;
    }

    return annView;
}
+8
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"id"];
if (pin == nil)
{
    pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"id"] ;
}
else
{
    pin.annotation = annotation;
}

pin.pinTintColor=[UIColor blueColor];
pin.canShowCallout = true;
0

All Articles