我有一个mapkit应用程序在地图上放置注释,当你按下它们时,它会显示带有title属性的标注。
这很好用,但用户无法关闭它们。他们保持开放,直到他们点击另一个注释。我不能认为用户可以在地图上点击其他地方(或再次点击注释)关闭它吗?
我有一种感觉这是默认设置,所以我正在做的事情就是填充它?我有一个手势识别器,我用它来检测一些地图点击
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吗?拖动和点击注释工作正常,但我不确定这是否是导致它的原因?
有一个我缺少的选项,还是我必须尝试手动实现?
答案 0 :(得分: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
,所以我们遍历它。
答案 1 :(得分: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];
}
}