I have been dealing with one problem for many hours and still cannot find out where's the problem. I have a mapView application in iOS where I show some polygons(zones) with a pin inside. After clicking on such annotation I want to display a calloutAnnotation. Only one such callout can be shown at a moment. That means, that after clicking on another zone annotation the one currently displayed must disappear and another should pop up. This is working pretty well, except that sometimes if I click on another annotation it gets selected and just afterwards it is deselected and another completely different annotation is selected. Here is the relevant piece of code:
- (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];
}
}
This should show the callout annotation
- (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];
}
Here is some logout output:
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
You can see, that it finds the clicked annotation first (that is Neuhausen-Nymphenburg) and also show the callout. But suddenly(and I have no idea why) the _showCalloutForAnnotation is also called for another zone (Altstadt-Lehel) causing the old one disappear.
It seems like something else triggers the _showCalloutForAnnotation, but I have checked my whole project and there are only three explicit calls to that method and none of them is causing this.
Thanks for any help.