2016-01-21 115 views
0

地圖上有註釋(可視標籤)。當用戶在地圖中縮放(放大或縮小)時,會調用名爲'regionDidChangeAnimated'的方法。每次完成時,我嘗試使用快速枚舉來查看哪些註記對象當前與地圖上的哪些註記對象相交。這是因爲我使用的是YandexMapKit(不是蘋果標準的MapKit),並且沒有太多關於如何以其他方式完成這些任務的文檔或示例。下面是唯一的代碼,我能想出這麼遠,但它最終崩潰的應用程序:比較兩個數組值CGRectIntersectsRect

NSArray *allAnnotations = [_mapView annotations]; 

// Check for intersecting annotation bounds. 
for (RBMapAnnotation *annotation1 in allAnnotations) 
{ 
    YMKPinAnnotationView *pinView1 = (YMKPinAnnotationView *)annotation1; // Still returns as 'RBMapAnnotation'. 

    for (RBMapAnnotation *annotation2 in allAnnotations) 
    { 
     YMKPinAnnotationView *pinView2 = (YMKPinAnnotationView *)annotation2; 

     if (CGRectIntersectsRect([pinView1 convertRect:pinView1.bounds toView:nil], [pinView2 convertRect:pinView2.bounds toView:nil])) 
     { 
      NSLog(@"Interesction between %@ and %@", annotation1.offer.name, annotation2.offer.name); 

      //[annotationsToUpdate addObject:annotation]; 
     } 

    } 
} 

請幫我出這一點。我試圖獲得的是,如果任何註釋在視覺上都在地圖上相交,那麼我將它們都刪除並用1個註釋替換它們。

+1

@Alex for適用於符合NSFastEnumeration的任何對象。 NSEnumerator符合NSFastEnumeration。 –

+0

我將其更改爲標準'[allAnnotations reverseObjectEnumerator]'爲'allAnnotations'。但它仍然崩潰。並不是說它應該有所作爲。 – Krekin

+0

提供有關崩潰的詳細信息。哪條線完全崩潰,錯誤信息是什麼? – rmaddy

回答

1

忽略算法效率低下(您可能要返工此,所以它不是O(N^2))...

你提到的代碼崩潰,但沒有提供任何診斷/回溯以協助診斷碰撞。

您正在循環註釋,將它們投射到YMKPinAnnotationView,並且他們將它們視爲這樣。這很可能是你的崩潰的根源。看來註釋是符合YMKAnnotation的實例,但實際上並不是視圖。您應該發送地圖視圖-viewForAnnotation:傳遞註釋以獲取關聯的視圖。

+0

謝謝。碰撞期間的診斷沒有任何有用的信息。但後來我發現它必須與鑄造到YMKPinAnnotationView。 – Krekin