1

I have been working on this problem for 6 hours and I am still struggling.

I have a mapview and I am adding MKPolygons like this:

for (MKPolygon *polygon in arrPolygon){
    [mapView addOverlay:polygon];
    [mapView addAnnotation:polygon];
}

I am finding which polygon overlay was tapped and select corresponding annotation programatically:

WildcardGestureRecognizer *tapges=[[WildcardGestureRecognizer alloc] init];
tapges.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {

    UITouch *touch = [touches anyObject];
    tappedOverlay = nil;
    if([touch tapCount]==1){
    for (id<MKOverlay> overlay in mapView.overlays)
    {
        MKOverlayView *view = [mapView viewForOverlay:overlay];
        if ([overlay isKindOfClass:[MKPolygon class]] && view)
        {
            // Get view frame rect in the mapView's coordinate system
            CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
            // Get touch point in the mapView's coordinate system
            CGPoint point = [touch locationInView:mapView];
            // Check if touch is within the view
            if (CGRectContainsPoint(viewFrameInMapView, point))
            {
                tappedOverlay = overlay;

                [mapView selectAnnotation:tappedOverlay animated:NO];
                break;
            }
        }
    }
    }
};

When I do this, Both didSelectAnnotationView and didDeselectAnnotationView are called for the same MKAnnotationView object. My question is, why Deselect method is being called?

When I manually select Annotation, It does not call Deselect method, meaning it works fine.

Thank You !!!

NetDeveloper
  • 509
  • 1
  • 9
  • 20

1 Answers1

1

Got solution myself. As Tap occurs on overlay but outside annotation boundary, Deselect method is called. Annotation that I select programatically in TouchesBegan, will be deselected because the method is called after Touchesbegan method is called.

NetDeveloper
  • 509
  • 1
  • 9
  • 20
  • 1
    Can you further expound on your solution. Using some code to illustrate your solution will be really helpful :) My team is having the same problem and we still don't know how to solve the problem. – alabid Jun 03 '14 at 19:44
  • When selectAnnotation method is called programatically, Deselect will be called if touch or tap occured outside annotation view boundary(because tap occured outside, mapview will invoke deselect after touches began method is called). So we again have to call selectAnnotation on our desired annotation. In didDeslectAnootattion method, add following code: if(view.annotation!= tappedOverlay){ [mapView selectAnnotation:tappedOverlay animated:NO]; } – NetDeveloper Jun 04 '14 at 14:02