ios mapkit通过点击地图来关闭注释标注 [英] ios mapkit closing annotation callouts by tapping the map
问题描述
我有一个mapkit应用程序在地图上放置注释,当你按下它们时,它会显示带有title属性的标注。
I've got a mapkit app that places annotations on the map, when you press them, it shows the callout with the title attribute.
这个工作正常,但用户无法关闭它们。它们保持打开状态直到它们点击另一个注释。我不能认为用户可以在地图上点击其他地方(或再次点击注释)来关闭它吗?
This works fine, but the user cannot close them. They stay open until they tap another annotation. Can't I make it that the user can tap elsehwere on the map (or tap the annotation again) to close it?
我有一种感觉这是默认设置,所以我正在做的事情就是填充它?我有一个手势识别器,用于检测一些地图点击
I had a feeling this was the default setting, so perhaps something I'm doing is stuffing it up? I have a gesture recognizer which I use to detect some map taps
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吗?拖动和点击注释工作正常,但我不确定这是否是导致它的原因?
Is there anything I have to do to also let these taps go through to the mapview? Dragging and tapping on annotations works fine though so I'm not sure if this is what's causing it?
有一个我缺少的选项,还是我必须尝试手动实现?
is there an option I'm missing, or do I have to try and implement this manually?
推荐答案
您可以实现 UIGestureRecognizerDelegate
方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
并返回 YES
(因此地图视图自己的点击手势识别器将执行其方法为好吧)。
You can implement the UIGestureRecognizerDelegate
method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
and return YES
(so the map view's own tap gesture recognizer will execute its method as well).
首先,将协议声明添加到视图控制器的界面(以避免编译器警告):
First, add the protocol declaration to your view controller's interface (to avoid a compiler warning):
@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>
接下来,设置委托
属性手势识别器:
Next, set the delegate
property on the gesture recognizer:
tap.delegate = self;
最后,实施方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
如果不起作用出于某种原因,你可以在 handleTap:
方法的顶部手动取消选择任何当前选择的注释:
If that doesn't work out for some reason, you can alternatively de-select any currently selected annotation manually at the top of the handleTap:
method:
for (id<MKAnnotation> ann in mapView.selectedAnnotations) {
[mapView deselectAnnotation:ann animated:NO];
}
即使地图视图只允许选择最多一个注释一段时间, selectedAnnotations
属性是 NSArray
所以我们循环遍历它。
Even though the map view only allows a maximum of one annotation to be selected at a time, the selectedAnnotations
property is an NSArray
so we loop through it.
这篇关于ios mapkit通过点击地图来关闭注释标注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!