我已经处理一个问题好几个小时了,但仍然无法找出问题所在。我在 iOS 中有一个 mapView 应用程序,我在其中显示了一些带有引脚的多边形(区域)。单击此类注释后,我想显示一个标注注释。一次只能显示一个这样的标注。这意味着,在单击另一个区域注释后,当前显示的一个必须消失,另一个应该弹出。这工作得很好,除了有时如果我单击另一个注释它被选中,然后它被取消选择并选择另一个完全不同的注释。这是相关的代码:
- (void)didTapMap:(NSSet *)touches {
CGPoint touchPoint = [touches.anyObject locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
NSLog(@"Checking for annotation");
VTParkingZoneAnnotation *tappedAnnotation = [self annotationAtCoordinate:touchMapCoordinate];
if (tappedAnnotation) {
NSLog(@"Annotation at coordinate: %@", tappedAnnotation.title);
[self.mapView setCenterCoordinate:[tappedAnnotation coordinate] animated:YES];
[self.mapView performSelector:@selector(selectAnnotation:animated:) withObject:tappedAnnotation afterDelay:0.0];
self.selectedAnnotation = tappedAnnotation;
} else {
NSLog(@"No annotation at coordinate");
self.selectedAnnotation = nil;
if (self.calloutAnnotation) [self.mapView removeAnnotation:self.calloutAnnotation];
[self.delegate mapSourceDidDeselectParkingZone:self];
}
}
这应该显示标注注释
- (void)_showCalloutForParkingZone:(VTParkingZone *)parkingZone {
NSLog(@"Showing callout for: %@", parkingZone.name);
VTCalloutAnnotation *callout = [[VTCalloutAnnotation alloc] init];
callout.parkingZone = parkingZone;
[self.mapView addAnnotation:callout];
self.calloutAnnotation = callout;
[self.delegate mapSource:self didSelectParkingZone:parkingZone];
MKCoordinateRegion rect = self.mapView.region;
rect.center = parkingZone.center;
[self.mapView setRegion:rect animated:YES];
}
这是一些注销输出:
2012-08-07 15:48:32.093 Viatag[34651:11603] Checking for annotation
2012-08-07 15:48:32.093 Viatag[34651:11603] Checking annotation: Baierbrunnerstrasse Contiparkplatz
2012-08-07 15:48:32.093 Viatag[34651:11603] Checking annotation: Baierbrunnerstrasse B2Xparkplatz
2012-08-07 15:48:32.094 Viatag[34651:11603] Checking annotation: Altstadt-Lehel
2012-08-07 15:48:32.094 Viatag[34651:11603] Checking annotation: Neuhausen-Nymphenburg
2012-08-07 15:48:32.094 Viatag[34651:11603] Annotation at coordinate: Neuhausen-Nymphenburg
2012-08-07 15:48:32.096 Viatag[34651:11603] Calling _showCalloutForAnnotation: Neuhausen-Nymphenburg
2012-08-07 15:48:32.096 Viatag[34651:11603] Showing callout for: Neuhausen-Nymphenburg
2012-08-07 15:48:32.446 Viatag[34651:11603] Calling _showCalloutForAnnotation: Altstadt-Lehel
2012-08-07 15:48:32.446 Viatag[34651:11603] Showing callout for: Altstadt-Lehel
您可以看到,它首先找到单击的注释(即 Neuhausen-Nymphenburg)并显示标注。但是突然(我不知道为什么) _showCalloutForAnnotation 也被称为另一个区域(Altstadt-Lehel),导致旧区域消失。
似乎其他东西触发了_showCalloutForAnnotation,但我检查了我的整个项目,只有三个对该方法的显式调用,而且没有一个导致这种情况。
谢谢你的帮助。