在iOS中显示标注和选择注释

时间:2012-08-07 13:53:10

标签: ios mapkit mkannotationview callouts

我已经处理了一个问题好几个小时但仍然无法找出问题所在。我在iOS中有一个mapView应用程序,我在其中显示了一些带有引脚的多边形(区域)。点击这样的注释后,我想显示一个calloutAnnotation。一时只能显示一个这样的标注。这意味着,在点击另一个区域注释后,当前显示的注释必须消失,另一个应该弹出。这很有效,除了有时如果我点击另一个注释它会被选中,之后它被取消选择并选择另一个完全不同的注释。以下是相关的代码:

- (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,但是我已经检查了我的整个项目,并且只有三个显式调用该方法,并且没有一个是由此引起的。

感谢您的帮助。

0 个答案:

没有答案
相关问题