2

I've an NSArray of custom MKAnnotation, i need to set a red pin color for the first/last annotation, otherwise set a green pin. The program does not behave as i want, the green pins are associated each time with two different annotations in a random way, are not associated with the first and the last as i want.

So, this is what i'm doing in my controller:

- (void)viewDidLoad
{   
    [super viewDidLoad];   
    //load set a coords from file etc...  
    //self.coordinates is the array of annotations   
    [self.myMap addAnnotations:self.coordinates];  
} 

then in the viewForAnnotation: callback:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    NSString *ident = @"MY_IDENTIFIER";
    MKPinAnnotationView *annView=(MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:ident];
    if(self.myMap.userLocation==annotation){
        return nil;
    }
    if(annView == nil){
        annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident];
        annView.animatesDrop=TRUE;
        annView.canShowCallout = YES;
        annView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.calloutOffset = CGPointMake(-5, 5);
        int currIdxAnn=[self.myMap.annotations indexOfObject:annotation];
        int lastIdxAnn=[self.myMap.annotations indexOfObject:[self.myMap.annotations lastObject]];
        /*
        if (currIdxAnn==0 || currIdxAnn==lastIdxAnn) {
            annView.pinColor=MKPinAnnotationColorRed;
        }else {
            annView.pinColor=MKPinAnnotationColorGreen;
        }*/
         CustomAnnotation *an=(CustomAnnotation *)annotation;
         if (an.tag==98||an.tag==99) {
            annView.pinColor=MKPinAnnotationColorGreen;
         }else {
            annView.pinColor=MKPinAnnotationColorRed;
         }

    }
    return annView;
}

It seems that the method addAnnotations: does not work as I believe, probably loads the annotations with an order different from the array. Is it possible?
I also tried to make a similar process in didAddAnnotationViews: but without good results.

Some hints? Thanks.

PS:as I finished writing this question i found this response, and seems to confirm my theory. Someone has already encountered a similar problem?

EDIT: After several tests i realized that the best way to accomplish what i want is to first set a tag to my first/last annotations:

CustomAnnotation *first=[self.coordinates objectAtIndex:0];
first.tag=98;
CustomAnnotation *last=[self.coordinates objectAtIndex:[self.coordinates indexOfObject:[self.coordinates lastObject]]];
last.tag=99;

Then as you can see i have slightly modified the viewForAnnotation to understand the annotation was the one I looked for. Then the trick was just calling the function that adds the annotations in a background thread, like:

[self performSelectorInBackground:@selector(addAllAnnot) withObject:nil];

-(void)addAllAnnot{

    [self.myMap addAnnotations:self.coordinates];
}

This worked for me after a week of testing, if anyone has any better idea, it will be appreciated.

Community
  • 1
  • 1
Mat
  • 7,613
  • 4
  • 40
  • 56

2 Answers2

2

Looking at the answer you referred in your answer I think there is no way by which you can determine the order in which these annotations are added or what index do they have in the internal list of the mapview.

So what option do you have..? As always the very fundamental-- Sub-classing the MKAnnotation class and adding a property to it through which you can differentiate which annotation is being added. So just set the property when you are adding these annotations in the array... hoping this solves your problem..

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • Thanks for the reply, i tried this, i am currently subclassing MKAnnotation, and i added an attribute tag, so i can store all tags in an array. But the strange thing is that if i insert a breakpoint in the if statement, the program performs all the steps correctly, but only after i go out of scope of the function, green pins are placed randomly. – Mat Apr 07 '12 at 17:57
0

I wouldn't count on the array given back by the map. It is probably optimised by mapkit in a way that's not obvious to you.

I'd keep a variable from the coordinates you calculated. Or If you don't want, just an array of the first and last annotations.

Cyril Godefroy
  • 1,400
  • 12
  • 15