更新注释的标记值而不删除&读取mapview中的注释

时间:2017-10-23 07:25:54

标签: ios mkmapview mkannotation mkannotationview

我需要在删除每个注释后更新所有注释标记。

您可能会问为什么不使用removeAnnotations& addAnnotation? 答案是,没有。引脚非常大(每个多边形约为3000)。所以删除所有注释&在地图上再次阅读这些内容会让人感到烦恼。所以我尝试了以下代码:

-(void)deletePin
{
    [MyMapView deselectAnnotation:currentAnnotationView.annotation animated:NO];
    //currentAnnotationView is the selected annotationView

    [self.arrAreaPin removeObjectAtIndex:currentAnnotationView.tag];
    //arrAreaPin holds all pin's properties(tag, title, pin color, etc)

    [MyMapView removeAnnotation:currentAnnotationView.annotation];

    if (currentAnnotationView.tag<self.arrAreaPin.count)
    {
        NSUInteger index = 0;
        for (id<MKAnnotation>annotation in MyMapView.annotations)
        {

            if (![annotation isKindOfClass:[MKUserLocation class]])
            {
                MyMapAnnotation *myAnn = (MyMapAnnotation *)annotation; //MyMapAnnotation is the custom MKAnnotation with some added properties like locationType(I stored the array index in it)
                myAnn.locationType = index; //Updating with array index after deleting a pin

                MKAnnotationView *annView = [MyMapView viewForAnnotation: annotation];
                NSLog(@"Before updating Annotation tag: %lu",annView.tag);
                annView.tag = index;

                NSLog(@"After updating Annotation tag: %lu",(unsigned long)annView.tag);
            }
            index++;
        }
    }
} 

但是看起来for循环从mapview中选择注释的方式与arrAreaPin数组的顺序不匹配。因此,当我在&amp;之前打印annotationView的标记值时更新annonation标签后,它看起来像:

Before updating Annotation tag: 0
After updating Annotation tag: 0

Before updating Annotation tag: 10
After updating Annotation tag: 1

Before updating Annotation tag: 16
After updating Annotation tag: 2

Before updating Annotation tag: 5
After updating Annotation tag: 3

Before updating Annotation tag: 17
After updating Annotation tag: 4

所以基本上看起来像for循环是从地图中随机选取注释。我对吗?

是否可以有序地更新注释的标签?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您使用的for in循环按照它们存储在MyMapView.annotations数组中的顺序处理数组元素。
你说你希望它们被处理“有序”。我假设您希望以tag顺序处理它们,这显然不是元素存储在数组中的顺序。因此,在处理它们之前,您必须按tag顺序对它们进行排序 所以我的建议是使用类似的东西(由于以下波兰人的评论而编辑过):

// Get your annotations (without the user location)
NSArray *myAnnotations = [MyMapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(MKAnnotation *annotation, NSDictionary *bindings) {
    return ![annotation isKindOfClass:[MKUserLocation class]];
}]];

// Sort your annotations by their tag
NSArray *sortedAnnotations = [myAnnotations sortedArrayUsingComparator:^NSComparisonResult(MKAnnotation *a, MKAnnotation *b) {
    MKAnnotationView *annViewA = [MyMapView viewForAnnotation: a];
    MKAnnotationView *annViewB = [MyMapView viewForAnnotation: b];
    return [[NSNumber numberWithInteger:annViewA.tag] compare:[NSNumber numberWithInteger:annViewB.tag]];
}];

// Update the tags
for (id<MKAnnotation>annotation in sortedAnnotations) {
    MyMapAnnotation *myAnn = (MyMapAnnotation *)annotation;
    myAnn.locationType = index;
    MKAnnotationView *annView = [MyMapView viewForAnnotation: annotation];
    NSLog(@"Before updating Annotation tag: %lu",annView.tag);
    annView.tag = index;
    NSLog(@"After updating Annotation tag: %lu",(unsigned long)annView.tag);
    index++;
  }