0

我添加注释如下:

 [mapView addAnnotations:annotationArray];  

然后我想访问 mapView 上的每个注释,如下所示:

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

问题是地图视图上的注释没有以链表的方式添加。这就是为什么它没有按照我向 annotationsArray 添加注释的顺序返回。

为了显示:,

 [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//

  }

mapView 注释中是否存在错误?

原始代码:

    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
4

2 回答 2

2

mapView 注释中是否存在错误?

除非您能找到一些说明地图视图将保留您添加注释的顺序的文档。MKMapView 很可能将它们存储在一些内部结构中,该结构可以有效地选择那些实际出现在地图上的注释。

不要使用地图视图作为存储注释的机制。以您喜欢的任何顺序自行存储它们,这样您就不必依赖 MKMapView 中不存在的行为。

于 2012-09-17T19:22:26.137 回答
0

MKMapView 将注解添加到其内部列表中,然后通过自己的逻辑显示这些注解视图。您可以通过 MKMapView 的属性“注释”检索注释列表。但是,没有直接的方法可以更改注释视图的 z 顺序。

于 2013-05-03T10:06:58.827 回答