2014-02-19 49 views
0

我成功地將自定義註釋添加到我的MKMapView併成功刪除它們。我正在使用子視圖控制器來過濾地圖視圖上的註釋。當我刪除所有註釋時,我在將新註釋添加到地圖視圖時遇到問題。正確的數據被添加到我的批註數組中,但是當我使用我的數組添加批註時,批註不會將其添加到地圖視圖中。任何幫助表示讚賞,謝謝。在iOS中,刪除所有自定義註釋並添加新的自定義註釋的正確方法是什麼?

這可能是值得一提的是,當我把自定義的註釋一個破發點if語句它被稱爲第一次我添加註釋到地圖視圖,而不是第二次。

這是我加上的註解:

for (placesChamber *placesObject in placesChamberArray) { 

    if ([placesObject.category isEqualToString:@"COC"]) { 

     tealAnnotation *annotationTeal = [[tealAnnotation alloc] init]; 

     CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([placesObject.latitude doubleValue],[placesObject.longitude doubleValue]); 


     annotationTeal.coordinate = coordinates; 

     annotationTeal.title = placesObject.name; 

     annotationTeal.tealAnnotationClass = placesObject; 

     [placesMapPointsArray addObject:annotationTeal]; 
    } 

} 

[placesMapView addAnnotations:placesMapPointsArray]; 

NSLog(@"Number of Places found: %lu",(unsigned long)placesMapPointsArray.count); 

NSLog(@"Number of Places on map: %lu",(unsigned long)placesMapView.annotations.count); 

if (!placesMapView.annotations || !placesMapView.annotations.count) { 
    NSLog(@"There are 0 annotations."); 

}//end if 

這是使用我的自定義註釋:(MKAnnotationView *)的MapView:(的MKMapView *)theMapView viewForAnnotation:(ID)註釋

if ([annotation isKindOfClass:[tealAnnotation class]]) // for 
    { 
     // try to dequeue an existing pin view first 
     static NSString *TealAnnotationIdentifier = @"tealPin"; 

     MKPinAnnotationView *tealPinView = 
     (MKPinAnnotationView *) [self.placesMapView dequeueReusableAnnotationViewWithIdentifier:TealAnnotationIdentifier]; 
     if (tealPinView == nil) 
     { 
      // if an existing pin view was not available, create one 
      MKPinAnnotationView *tealAnnotationView = [[MKPinAnnotationView alloc] 
                 initWithAnnotation:annotation reuseIdentifier:TealAnnotationIdentifier]; 

      tealAnnotationView.image = [UIImage imageNamed:@"teal_pin.png"]; 
      tealAnnotationView.canShowCallout = YES; 
      tealAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

      CGSize newSize; 
      newSize.width = 32; 
      newSize.height = 32; 
      UIGraphicsBeginImageContext(newSize); 
      [tealAnnotationView.image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
      UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
      UIGraphicsEndImageContext(); 

      tealAnnotationView.image = newImage; 

      return tealAnnotationView; 
     } 
     else 
     { 
      tealPinView.annotation = annotation; 
     } 
     return tealPinView; 
    } 

以下是我如何刪除註釋:

[placesMapView removeAnnotations:placesMapPointsArray]; 
[placesMapPointsArray removeObjectsInArray:placesMapPointsArray]; 

獲取信息後fr om註釋我已經注意到,我的mapView的viewDelegate已經變爲「無」。

+0

嘗試更改'[placesMapView removeAnnotations:placesMapPointsArray];'''[placesMapView removeAnnotations:placesMap.annotations];' – JoeFryer

+0

@JoeFryer沒有工作。仍然沒有向地圖視圖添加註釋。 – mcphersonjr

+0

哪個if語句沒有被第二次觸發? –

回答

1

按照你的意見的問題顯然是placesChamberArray沒有任何物體與「COC」對第二次運行一個類別。如果這樣做的placesMapPointsArray必須有一些項目,但NSLOg聲明說它沒有。您可以通過在isEqualToString之後放置NSLog並顯示它未運行來驗證此情況。

+0

Craig,謝謝你的回答,但placesChamberArray用for循環創建placesMapPointsArray。我能夠看到PlacesMapPointsArray具有正確數量的對象,正確的數據並將其應用於正確的tealAnnoationClass。但它永遠不會到達地圖。 – mcphersonjr

+1

我誤解了你的評論。你說placesMapPointsArray確實有項目,但是調用addAnnotations然後annotations.count返回0.這對我來說聽起來是不可能的。這裏的工作必須有更多的代碼。 – Craig

+0

原來,這是我用於我的過濾器的[ECSlidingViewController](https://github.com/ECSlidingViewController/ECSlidingViewController)。一旦我刪除它,它的工作。我認爲它沒有授權我的地圖視圖,也不允許我放置新的註釋。 – mcphersonjr

0

你有沒有再打電話[placesMapView addAnnotations:placesMapPointsArray]你添加新的對象到數組後?

+0

請參考** Craig's **的問題,作爲回答您的回答問題。 – mcphersonjr