2011-06-23 40 views
6

我有一个mapkit应用程序,它可以在地图上放置注释,当你按下它时,它会显示标题属性的标注。ios mapkit通过点击地图关闭注释标注

这工作正常,但用户无法关闭它们。他们保持开放状态,直到他们再次注释为止。我不能让用户点击地图上的其他地方(或再次点击注释)关闭它吗?

我有一种感觉,这是默认设置,所以也许我正在做的东西是填塞它?我有我用它来检测一些地图水龙头

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


    } 
} 

}

有什么我必须做的,也让这些水龙头经历到mapview?在注释上拖动和点击效果很好,但我不确定这是什么原因造成的?

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

回答

10

您可以实施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,所以我们通过它循环。

0

安娜解释好,我也想用完全的代码解释。你可以这样做

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


     }