ios mapkit通过点击地图来关闭注释标注

Rod*_*257 6 objective-c mapkit ios

我有一个mapkit应用程序在地图上放置注释,当你按下它们时,它会显示带有title属性的标注.

这工作正常,但用户无法关闭它们.他们保持开放,直到他们点击另一个注释.我不能认为用户可以在地图上点击其他地方(或再次点击注释)关闭它吗?

我有一种感觉这是默认设置,所以我正在做的事情就是填充它?我有一个手势识别器,我用它来检测一些地图点击

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 1;

[self.mapView addGestureRecognizer: tap];
Run Code Online (Sandbox Code Playgroud)

这解雇了这个:

- (void)handleTap:(UITapGestureRecognizer *)sender {     
if (sender.state == UIGestureRecognizerStateEnded)  {   


    CGPoint tapPoint = [sender locationInView:sender.view.superview];
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint: tapPoint toCoordinateFromView: self.mapView];

    if (pitStopMode && !pitStopMade){
            pitStopMade = YES;
        InfoAnnotation *annotation = [[InfoAnnotation alloc]
                       initNewPitstopWithCoordinate:coordinate];
        NSLog(@" Created Pit Stop");

        annotation.draggable = NO;
        //place it on the map
        [self.mapView addAnnotation: annotation];

        self.instructionLabel.text = @"Tap button again to remove";
        annotation.creatorId = self.localUser.deviceId;
        //send it to the server
        [annotation updateLocationWithServerForConvoy: self.convoyCode];        

        [annotation release];

    }  

    if (hazardMode && !hazardMade){
            hazardMade = YES;
        InfoAnnotation *annotation  = [[InfoAnnotation alloc]
                       initNewHazardWithCoordinate:coordinate];         
        NSLog(@" Created Hazard");

        annotation.draggable = NO;
        //place it on the map
        [self.mapView addAnnotation: annotation];

        self.instructionLabel.text = @"Tap button again to remove";
        annotation.creatorId = self.localUser.deviceId;
        //send it to the server
        [annotation updateLocationWithServerForConvoy: self.convoyCode];        

        [annotation release];


    }
}
Run Code Online (Sandbox Code Playgroud)

}

有什么我需要做的也让这些水龙头进入mapview吗?拖动和点击注释工作正常,但我不确定这是否是导致它的原因?

有没有我缺少的选项,还是我必须尝试手动实现?

小智 10

您可以实现该UIGestureRecognizerDelegate方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:并返回YES(因此地图视图自己的点击手势识别器也将执行其方法).

首先,将协议声明添加到视图控制器的接口(以避免编译器警告):

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
Run Code Online (Sandbox Code Playgroud)

接下来,delegate在手势识别器上设置属性:

tap.delegate = self;
Run Code Online (Sandbox Code Playgroud)

最后,实现方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:
        (UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)



如果由于某种原因无法解决问题,您可以在handleTap:方法顶部手动取消选择任何当前选定的注释:

for (id<MKAnnotation> ann in mapView.selectedAnnotations) {
    [mapView deselectAnnotation:ann animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

即使地图视图一次只允许选择一个注释,该selectedAnnotations属性也是NSArray如此,我们循环使用它.