Blog

ZSPinAnnotation: Custom MKMapView pin images from UIColor

Wed, Dec. 7, 2011

Have you ever felt limited by the three Annotation pin colors that Apple provides with MapKit? Have you ever thought it was a pain to have to create custom images in photoshop every time you wanted to change the color? ZSPinAnnotation solves these problems by building the pin image on the fly. All you have to do is specify a UIColor and you get back a UIImage.

  • ZSImageView

If you want to create a ZSPinAnnotation you simply do the following:

UIImage *img = [ZSPinAnnotation pinAnnotationWithColor:[UIColor blueColor]];

The full implementation in mapView:viewForAnnotation:

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

    if(![annotation isKindOfClass:[Annotation class]]) // Don't mess user location
        return nil;

    static NSString *defaultPinID = @"StandardIdentifier";
    MKAnnotationView *pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (pinView == nil){
        pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        //pinView.animatesDrop = YES;
    }

    // Build our annotation
    if ([annotation isKindOfClass:[Annotation class]]) {
        Annotation *a = (Annotation *)annotation;
        pinView.image = [ZSPinAnnotation pinAnnotationWithColor:a.color];// ZSPinAnnotation Being Used
        pinView.annotation = a;
        pinView.enabled = YES;
        pinView.calloutOffset = CGPointMake(-11,0);
    }

    pinView.canShowCallout = YES;

    return pinView;

}//end

Nicholas Hubbard
Owner

Comments

wow
Nov 24, 2013
can't believe no comments. let me say it--- wonderful code, thank you.
George
Feb 23, 2015
I can't seem to activate calloutAccessoryControlTapped when the popup is pressed
Nicholas Hubbard
Feb 23, 2015
Can you create a new issue for this on Github?
ansni
Apr 30, 2015
i need a help to show multiple image on annotation in mampview
Nicholas Hubbard
Apr 30, 2015
Can you please create an issue on Github?

Add Comment