Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deselect a map annotation on second tap

My task is to deselect a map annotatnion on second tap.

I didn't find how to do it with mapView functions. So I used an article from stackoverflow and do like this:

- (void)viewDidLoad
{
    annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationTapRecognized:)];
    annotationTap.numberOfTapsRequired = 1;
    annotationTap.delegate = self;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [view addGestureRecognizer:annotationTap];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [view removeGestureRecognizer:annotationTap];
}

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture
{
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
    for (MapAnnotation *annotationView in selectedAnnotations) {
        [self.viewMap deselectAnnotation:annotationView animated:NO];
    }
}

It seems works correct, but it is not. When I tap on the annotation second time callout disappears and appears again.

Any ideas?

Thanks in advance.

like image 605
Igor Avatar asked Jun 29 '12 14:06

Igor


2 Answers

I've found the solution. Maybe it's not good.

I've added boolean "is show", as luxsypher mentioned. So my functions looks like the following:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [view addGestureRecognizer:annotationTap];

    if (isShow) {
        NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
        for (MapAnnotation *annotationView in selectedAnnotations) {
            [self.viewMap deselectAnnotation:annotationView animated:YES];
        }
        isShow = FALSE;
    }
}

- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture
{
    NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
    for (MapAnnotation *annotationView in selectedAnnotations) {
        [self.viewMap deselectAnnotation:annotationView animated:YES];
    }
    isShow = TRUE;
}

Maybe it will be useful for somebody :).

Thanks.

like image 157
Igor Avatar answered Nov 14 '22 22:11

Igor


Maybe you should add a boolean "is visible" and act consequently. Cause it looks like you gesture is called and then "did Select" is called again.

like image 20
Damien Locque Avatar answered Nov 14 '22 20:11

Damien Locque