2011-11-21 108 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