0

I add my annotations as follows:

 [mapView addAnnotations:annotationArray];  

then I want to access each and individual annotations on the mapView as follows:

for (id<MKAnnotation> ann in mapView.annotations)

the problem is annotations on map view is not added in a way linked list. That is why it is not returning in same order that I add annotations to annotationsArray.

to illustrate:,

 [annotationArray addObject: annotation1]; 

 [annotationArray addObject: annotation2];

 [annotationArray addObject: annotation3];

 [mapView addAnnotations:annotationArray];  

 for (id<MKAnnotation> ann in mapView.annotations)
 {

  //not returning in same order as added above//

  }

Is there a bug in mapView annotation?

original code:

    NSMutableArray *attrArray=[[NSMutableArray alloc]init];
for (NSArray *row in latt)
{
    NSNumber *attr=[row valueForKey:@"attr"];
    NSNumber *attr2=[attr valueForKey:@"ozone_level"];
    NSLog(@"values:%@",attr2);
    NSNumber *comp=[NSNumber numberWithInt:-1];
    if(attr2!=comp)
    {
        if([attrArray count]==0)
        {attrArray=[[NSMutableArray alloc]initWithObjects:attr2, nil];
        }
        else
        {
            [attrArray addObject:attr2];
        }
        NSNumber *latt2=[row valueForKey:@"lat"];
        NSNumber *longt2=[row valueForKey:@"lng"];
        CLLocationCoordinate2D coordinate;
        coordinate.latitude=latt2.doubleValue;
        coordinate.longitude=longt2.doubleValue;
        if(test<5)
        {
        Annotation *annotation = [[Annotation alloc] init];
        annotation.coordinate = coordinate;
        annotation.title = [attr2 stringValue];
        int colorvalue=[attr2 intValue];
        pinColor = [self colorForAcceleration2:colorvalue];
        annotation.color = pinColor;

            if([annotationArray count]==0)
            {
                annotationArray=[[NSMutableArray alloc]initWithObjects:annotation,nil];
               // NSMutableArray *ad =[[NSMutableArray alloc]init];
            }
            else 
            {
                [annotationArray addObject:annotation];

            }

        }

        test++;

    }//if attr2
}//for
if( test == 5)
{
  [mapView addAnnotations:annotationArray];  
}
if(test>=5)
{
    //for (id<MKAnnotation> ann in mapView.annotations)
    for(int i=0;i<5;i++)  
    {   //Annotation *a = (Annotation *)ann;
         Annotation *a = ((Annotation*)[annotationArray objectAtIndex:i]);

        //((Annotation *)mapView.annotations).
        NSLog(@"%@",((Annotation*)[annotationArray objectAtIndex:i]).title);
        NSLog(@"%@",[attrArray objectAtIndex:i]);
        //NSLog(@"annotation array size:%d",[annotationArray count]);
       // NSLog(@"attr array size:%d",[attrArray count]);
        if([((Annotation*)[annotationArray objectAtIndex:i]).title isEqualToString:[[attrArray objectAtIndex:i] stringValue]])
        {
        }
        else{
            MKAnnotationView *av1=[mapView viewForAnnotation:((Annotation*)[annotationArray objectAtIndex:i])];
                a.title=[NSString stringWithFormat:[[attrArray objectAtIndex:i] stringValue]];
                int colorvalue=[[attrArray objectAtIndex:i] intValue];
                pinColor = [self colorForAcceleration2:colorvalue];
                a.color=pinColor;
                av1.image=[ZSPinAnnotation pinAnnotationWithColor:a.color];
                av1.annotation=a;}//else

        }//for
    }//if
casillas
  • 16,351
  • 19
  • 115
  • 215
  • 2
    It's not a "bug" -- it's "by design". Why do you need them in the same order? Related: http://stackoverflow.com/a/9543136/467105 –  Sep 17 '12 at 18:58
  • because I want to access them in same order in other for loop. if(Annotation*)[annotationsArray objectAtIndex=j].title isEqualtoString [[newDataArray objectAtIndex:j]stringValue]{}else{update pin title} – casillas Sep 17 '12 at 19:01
  • 1
    Please update the question with the exact code you're trying to use in the other loop. Code in your comment doesn't show how you're trying to use the `ann` object. –  Sep 17 '12 at 19:07
  • I am using ann object in the else loop to update title.MKAnnotationView av=[mapView viewforAnnotaion: ann] – casillas Sep 17 '12 at 19:09
  • Hello Anna,code posted above. please see commented out part. it was not working. I commented out that part and implemented for loop with (int i=0) manner. The scenario is to have 5 pins added on map view and check whether or not values of the pin changed during time. I am first adding my pins on map view and then checking whether the values of pins change or not? – casillas Sep 17 '12 at 19:16
  • to make it easy to understand, latt object in the for loop above just gets data from server, it has lat,langt and temp value, nothing else. – casillas Sep 17 '12 at 19:22

2 Answers2

2

Is there a bug in mapView annotation?

Not unless you can find some documentation that says a map view will preserve the order in which you added the annotations. MKMapView very probably stores them in some internal structure that can efficiently select just those annotations that will actually appear on the map.

Don't use a map view as your mechanism for storing annotations. Store them yourself in whatever order you like, so that you don't have to rely non-existant behavior in MKMapView.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • That is way I created custom for loop to avoid this confusion in my code shown above. But that is obviously kind a strange! I assumed thta my annotation added as linked list then I can recall them in same order. I have been dealing this issue for two days and figure that out and came up this conclusion. Thanks a lot Caleb, appreciated! – casillas Sep 17 '12 at 19:26
0

MKMapView add annotations to its internal list, then display these annotation views by its own logic. You can retrieve the list of annotations by MKMapView's property "annotations". However there is no straight method to change the z-order of annotation views.

hrchen
  • 1,223
  • 13
  • 17