Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

objective c - Map view annotations with different pin colors

I have an array with over 200 objects and I am trying to perform a loop through each of them.

Each object will have a yes/no field and I want to display a different coloured marker dependent on that yes / no value.

From what I can see is happening my loop is going through each object first and then all the annotation is added at the end for each object .

Since I perform a check within my loop through the array on the yes no value when all the annotation is added to my map, it will use the yes/no value from the last object in the array when it goes to plot for all.

How can I have it so that the marker will be different dependent on the yes/no value for each individual element?

My code is

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;

}

and current_yesno is declared in my .h file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The viewForAnnotation delegate method isn't necessarily called immediately after you do addAnnotation and it can also be called at other times by the map view when it needs to get the view for an annotation (while your code is doing something completely different).

So you can't depend on the value of an ivar being in sync with some code outside that delegate method.

Instead, add the yesno property to your custom MapViewAnnotation class, set it when creating the annotation and then access its value in viewForAnnotation through the annotation parameter (ie. the map view is giving you a reference to the exact annotation object it wants the view for).

Example:

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

Then in 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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...