Code: Custom pins in your map

By December 23, 2013 May 25th, 2020 No Comments

Sometimes you need to add a custom pin to your map. You can use this piece of code to achieve that.

Take a special look to the property “centerOffset” that allows to adjust your image from the center of your MKAnnotationView (usually you will set it as (0 , minus half of the height of your image).

An example without correcting the centerOffset property:


And this one correcting the centerOffset and setting it to (0,-halfImage):

Captura de pantalla 2014-01-04 a la(s) 13.08.51

Be careful with the class you use for adding the pin to the map. To be able to set the centerOffset you need to use MKAnnotationView, instead of MKPinAnnotatioView… because in this one the property centerOffset works in an unpredictable way.

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

    if ([annotation isKindOfClass:[CODAnnotation class]]) {

        static NSString *identifier = @"myIdentifier";
        MKAnnotationView *annotationView = (MKAnnotationView *) [_map dequeueReusableAnnotationViewWithIdentifier:identifier];

            //Lazy instantation
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.image = [UIImage imageNamed:@"custom_image"];//here we use a nice image instead of the default
            annotationView.centerOffset = CGPointMake(0,-annotationView.frame.size.height*0.5);

        } else {

            annotationView.annotation = annotation;


        return annotationView;


    return nil;