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];
}
}
}
我需要做些什么才能让这些点击进入地图视图吗?拖动和点击注释效果很好,所以我不确定这是否是造成这种情况的原因?
我是否缺少一个选项,或者我是否必须尝试手动实施此选项?
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];
which fires this:
- (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];
}
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以实现
UIGestureRecognizerDelegate
方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
并返回YES
(因此地图视图自己的点击手势识别器也将执行其方法)。首先,将协议声明添加到视图控制器的接口中(以避免编译器警告):
接下来,在手势识别器上设置
delegate
属性:最后,实现方法:
如果由于某种原因这不起作用,您也可以在
handleTap:
方法顶部手动取消选择任何当前选定的注释:即使地图视图最多只允许一个注释要一次选择,
selectedAnnotations
属性是一个NSArray
,因此我们循环遍历它。You can implement the
UIGestureRecognizerDelegate
methodgestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
and returnYES
(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):
Next, set the
delegate
property on the gesture recognizer:Finally, implement the method:
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:Even though the map view only allows a maximum of one annotation to be selected at a time, the
selectedAnnotations
property is anNSArray
so we loop through it.安娜解释得很好。我也想用确切的代码来解释。你可以这样做
Anna explain good.Also I want to explain with exactly code. You can do like this