2017-10-23 3 views
0

모든 주석을 제거한 후 모든 주석 태그를 업데이트해야한다는 요구 사항이 있습니다.지도 뷰에서 주석을 제거하고 읽지 않고 주석의 태그 값 업데이트

왜 사용하지 않는지 묻는 질문이 있습니다. 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++; 
     } 
    } 
} 

를하지만 루프에 대한이 arrAreaPin 배열의 순서와 일치하지 않는지도보기에서 주석을 선택합니다 길처럼 보인다. 루프에 대한지도에서 무작위로 주석을 따기 같은 기본적

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 

외모 : 나는 annonation 태그를 업데이트 한 후 & 전에 annotationView의 태그 값을 인쇄하고있을 때 그래서 같이 보인다. 내가 맞습니까?

주석 태그를 순서대로 업데이트 할 수 있습니까? 미리 감사드립니다.

답변

0

사용하는 for in 루프는 MyMapView.annotations 배열에 저장된 순서대로 배열 요소를 처리합니다.
"orderly"으로 처리하기를 원한다고 말했습니까? 나는 당신이 그들의 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++; 
    } 
+0

고마워 :
그래서 내 제안은 같은 (아래 폴란드 의견 인해 편집 된)를 사용하는 것입니다. 그것은 내가 생각했던 것 같았다. 그러나 작은 변화가 필요했습니다. 'sortedArrayUsingComparator'에서 우리는'[annViewA.tag compare : annViewB.tag] 대신'return [[NSNumber numberWithInteger : annViewA.tag] compare : [NSNumber numberWithInteger : annViewB.tag]];를 사용할 필요가 있습니다. – Poles

+0

@ 폴란드 감사합니다. 위에서 제안한대로 코드를 편집했습니다. –

관련 문제