【问题标题】:ios mapkit closing annotation callouts by tapping the mapios mapkit 通过点击地图关闭注释标注
【发布时间】:2011-06-23 05:20:53
【问题描述】:

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

这工作正常,但用户无法关闭它们。他们保持打开状态,直到他们点击另一个注释。我不能让用户可以在地图上点击 elsehwere(或再次点击注释)来关闭它吗?

我感觉这是默认设置,所以也许我正在做的事情是把它塞进去?我有一个手势识别器,我用它来检测一些地图点击

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

[self.mapView addGestureRecognizer: tap];

触发这个:

- (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];


    }
}

}

我还需要做什么才能让这些点击进入地图视图吗?拖动并点击注释可以正常工作,所以我不确定这是否是导致它的原因?

是否有我遗漏的选项,或者我必须尝试手动实现它?

【问题讨论】:

    标签: objective-c ios mapkit


    【解决方案1】:

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

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

    @interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
    

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

    tap.delegate = self;
    

    最后,实现方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer:
            (UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    



    如果由于某种原因这不起作用,您也可以在 handleTap: 方法的顶部手动取消选择任何当前选择的注释:

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

    尽管地图视图一次最多只能选择一个注释,但selectedAnnotations 属性是NSArray,所以我们循环遍历它。

    【讨论】:

      【解决方案2】:

      安娜解释得很好。我也想用确切的代码来解释。你可以这样做

           UITapGestureRecognizer *tapMap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeCallout:)];
            [self.mapView addGestureRecognizer:tapMap];
      
      -(void) closeCallout:(UIGestureRecognizer*) recognizer 
              {
                 for (id<MKAnnotation> ann in mapView.selectedAnnotations) 
                 {
                     [mapView deselectAnnotation:ann animated:NO];
                 }      
      
      
              }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-19
        • 2011-11-05
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多