Aswathy Bose
Aswathy Bose

Reputation: 4289

Custom image for annotation pin in mkmapview

Custom annotation pin changes to default red pin at long tap.

  - (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
 MKPinAnnotationView *annView = nil;
        if(annotation != mapingView.userLocation) 
        {

            static NSString *defaultPinID = kDEFAULTPINID;
            annView = (MKPinAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
            if ( annView == nil ) 
            annView = [[MKPinAnnotationView alloc]
                                             initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;


            annView.canShowCallout = YES;
            annView.image = [UIImage imageNamed:@"icon.png"];//sets image for default pin

}
return annView;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view 
    { 
        if ([[view.annotation title] isEqualToString:@"AnnotationTitle"] ) {

             view.image = [UIImage imageNamed:@"selected_IconImage.png"];
       }
    }

Just touching the annotation pin the selected image appears. But on long tap on the pin custom image reverts to default red pin.

How to fix this issue?

Upvotes: 2

Views: 3758

Answers (1)

wattson12
wattson12

Reputation: 11174

Use an MKAnnotationView instead of an MKPinAnnotationView, I'm guessing the map view performs some sort of reset, which goes back to the default image (which is the pin image you see)

Upvotes: 12

Related Questions