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 に答える