AltafBangash
AltafBangash

Reputation: 129

MapView delegate issue

I'm working on MapView. The mapView delegate method didSelectAnnotationView get called when I tap on Pin but when I tap on Pin again this method does not get called until I don't click on map.

I want whenever I click on location Pin the method didSelectAnnotationView get called. Please help

This is didSelectAnnotationView method:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView{
    id<MKAnnotation> annotation = aView.annotation;
    if (!annotation || ![aView isSelected])
        return;

    if ([[annotation title] isEqualToString:@"My Location"])
        return;

    NSLog(@"Annotation Title: %@",[annotation title]);

    if(self.calloutAnnotation != nil)
    {        
        [pMapView removeAnnotation:self.calloutAnnotation];
        self.calloutAnnotation = nil;
        return;
    }

    if ( NO == [annotation isKindOfClass:[MultiRowCalloutCell class]])
    {
        NSObject <MultiRowAnnotationProtocol> *pinAnnotation = (NSObject <MultiRowAnnotationProtocol> *)annotation;
        if (!self.calloutAnnotation)
        {
            self.calloutAnnotation = [[MultiRowAnnotation alloc] init];
            [self.calloutAnnotation copyAttributesFromAnnotation:pinAnnotation];
            [mapView addAnnotation:self.calloutAnnotation];
        }

        self.selectedAnnotationView = aView;
        return;
    }

    [mapView setCenterCoordinate:annotation.coordinate animated:YES];
    self.selectedAnnotationView = aView;
}

Upvotes: 2

Views: 266

Answers (2)

Nikita Khandelwal
Nikita Khandelwal

Reputation: 1741

#pragma mark MapView Delegate Methods

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
   {
        indexPathTag=aView.tag;
        [mapView deselectAnnotation:aView.annotation animated:YES];

    }
 - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)aView
    {
    }

I have faced the same problem.This worked for me :) Hope this will work for u too. Thanks

Upvotes: 0

Hiren
Hiren

Reputation: 686

I was having same problem but I solved it. Call didDeselectAnnotationView method and remove it from the superview.Like this:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
for (UIView *subview in view.subviews ){
    [subview removeFromSuperview];
    }
}

It worked for me,hope it works for you.

Upvotes: 1

Related Questions