Shows different Pin images in MKMapview

user2000452 picture user2000452 · Feb 21, 2013 · Viewed 11.5k times · Source

In my MKMap view I have customized the annotation pin with an image. But still some pins are static and not showing the given image.

I am using -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation to set the pin image.

Adding my code and screen here:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
    pinView.pinColor= MKPinAnnotationColorGreen;

    pinView.enabled = YES;
    pinView.canShowCallout = YES;
    pinView.image=[UIImage imageNamed:@"bublerest.png"]; //here I am giving the image  





UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];

    pinView.rightCalloutAccessoryView = rightButton;

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rest_image2.png"]];
pinView.leftCalloutAccessoryView = profileIconView;


return pinView;
}

enter image description here

Any ideas?

Answer

blub picture blub · Feb 21, 2013

If you want a different image on the MKAnnotationView than apples "Pin", you have to use a MKAnnotationView instead of a MKPinAnnotationView.

MKPinAnnotationView is not meant to be customized this way and will keep showing the Pin from time to time. The Class Reference shows that only pinColor and animatesDrop are supposed to be changed.

You will lose the PinAnnotationView's animation and shadow, though.