2011-11-21 81 views
0

我試圖刪除放置在地圖上的自定義註釋。問題是,爲了刪除該註釋,用戶必須按兩次我所做的刪除按鈕。這是刪除註釋的代碼行,[mv removeAnnotation:[mv.selectedAnnotations objectAtIndex:0]]。我幾乎肯定,默認情況下我的當前位置(MKUserLocation)始終是數組中的第一個註釋,這就是爲什麼我必須按刪除兩次。我在正確的軌道上嗎?任何意見,將不勝感激。在iOS中刪除自定義註釋時遇到問題

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
NSLog(@"\n\n\n################################\nANNOATION SELECTED\n################################"); 



_currentAnnotation = view.annotation; 
[self zoomOnAnnotation:_currentAnnotation]; 

NSLog(@"selectedannotationview is %@", _currentAnnotation); 

[viewLoadingCover setHidden:FALSE]; 
[actIndLoading startAnimating]; 
//Annotation *currentAnnotation = view.annotation; 
currentAnnoView = view; 
//if (isLoadingCallout) { 
    [self getCurrentMapDataWithLatitute:_currentAnnotation.coordinate.latitude Longitute:_currentAnnotation.coordinate.longitude]; 
//} 


if ([view.annotation isEqual:mapView.userLocation]) 
{ 
    isUser = YES; 
    //NSLog(@"##### yes this is user location annotation"); 
    if (!view.rightCalloutAccessoryView) { 
     //NSLog(@"##### yes view.rightCalloutAccessoryView is NIL"); 
     mapView.userLocation.title = @"Loading..."; 
     mapView.userLocation.subtitle = @" "; 
     //NSLog(@"##### cityAndState(subtitle) = %@", cityAndState); 
     //[[mapView userLocation] setSubtitle:[NSString stringWithString:cityAndState]]; 
     userButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     view.rightCalloutAccessoryView = userButton; 
     [userButton addTarget:self action:@selector(UIButton:) forControlEvents:UIControlEventTouchUpInside]; 
     NSLog(@"userlocation selected---------------------------%@", cityAndState); 
    } 

    reverseG= [[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.coordinate]; 
    reverseG.delegate = self; 
    [reverseG start]; 
} 
else 
{ 
    isUser = NO; 
} 

} 

回答

0

你有興趣在委託方法的MKMapView

- (Void) mapView: (MKMapView *) mapView didSelectAnnotationView: (MKAnnotationView *) view; 
- (Void) mapView: (MKMapView *) mapView didDeselectAnnotationView: (MKAnnotationView *) view; 

通過他們,你會知道什麼註解選擇或取消選擇。

+0

感謝Aliaksandr,我發佈了我的didSelectedAnnotationView。我認爲我已經釘住了,因爲我發送了SelectedAnnotation,但由於某種奇怪的原因,它仍然需要2次嘗試。 – user1042402

1

我建議NSLogging mv.selectedAnnotations計數,看看發生了什麼。當計數> 1時,不會刪除索引1而不是0的註釋,這很快證明您的理論是正確的還是錯誤的?

+0

是的,Samuli你是對的。當我嘗試使用索引1時,它因爲超出限制而崩潰。當我輸出數組'NSLog(@「Array for annot =%@」,self.annotations);'我實際上獲得了超過300行註釋,實際上只有大約4個已經繪製在地圖上。也許它會在放大時更新註釋。這聽起來可行,因爲註釋的經緯度將會改變,直到達到所需的縮放級別。 - [__ NSArrayI objectAtIndex:]:index 1 beyond bounds [0 .. 0]' – user1042402

相關問題