Reputation: 16793
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
Upvotes: 0
Views: 501
Reputation: 1223
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.
Upvotes: 0
Reputation: 124997
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.
Upvotes: 2