ios mapkit 通过点击地图关闭注释标注

发布于 2024-11-17 00:59:40 字数 2180 浏览 0 评论 0原文

我有一个地图套件应用程序,可以在地图上放置注释,当您按下它们时,它会显示带有标题属性的标注。

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

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

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-11-24 00:59:40

您可以实现 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,因此我们循环遍历它。

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;

Finally, implement the method:

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

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

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.

梦旅人picnic 2024-11-24 00:59:40

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

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


        }   

Anna explain good.Also I want to explain with exactly code. You can do like this

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


        }   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文