Custom pin on MKMapView in iOS?

I have tried just about everything to get an image displayed instead of the default red pin on my MKMapView.

There are a lot of answers about this on the internet but all of them keep giving me this:

  • How to play keyboard click sound in custom keyboard?
  • heat map on map view in iphone…?
  • Xcode: Process launch failed - Security
  • Swift nil is not the same as Objective-C nil
  • Dynamically accessing local variables in Objective-C runtime
  • Adding Navigation Bar programmatically iOS

  • - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        NSString *annotationIdentifier = @"CustomViewAnnotation";
        MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        if(!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:annotationIdentifier]];
        }
        annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
        annotationView.canShowCallout= YES;
    
        return annotationView;
    }
    

    This doesn’t help me at all, its just a method by the looks of it.

    Please tell me how to use this to make the method return a annotation with a custom image.
    Just about all the other answers tell me to implement that method with no further explaination.

    Where do I call it? How do I call it?

    Thanks!

    Solutions Collect From Internet About “Custom pin on MKMapView in iOS?”

    Managed to get this fixed on my own, here is what I did in 4 steps:

    Step 1: You have to set the delegate for the mapview to the ViewController:

    MapViewController.h

     @interface MapViewController : UIViewController <MKMapViewDelegate> {
            IBOutlet MKMapView *mapView;
        }
    @property (nonatomic, nonatomic) IBOutlet MKMapView *mapView;
    

    MapViewController.m

    - (void)viewDidLoad
    {    
        [super viewDidLoad];
        [mapView setDelegate:self];
    }
    

    Step 2: Implement the method:

    MapViewController.m

    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
        MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
        //MyPin.pinColor = MKPinAnnotationColorPurple;
    
    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    
    /*MyPin.rightCalloutAccessoryView = advertButton;
     MyPin.draggable = YES;
    
     MyPin.animatesDrop=TRUE;
     MyPin.canShowCallout = YES;*/
    MyPin.highlighted = NO;
    MyPin.image = [UIImage imageNamed:@"myCustomPinImage"];
    
    return MyPin;
    }
    

    Step3: Create a class called “Annotation” (or any other name)

    Annotation.h

    #import <Foundation/Foundation.h>
    #import <MapKit/MKAnnotation.h>
    
    @interface Annotation : NSObject <MKAnnotation> {
    
        CLLocationCoordinate2D coordinate;
        NSString *title;
        NSString *subtitle;
    
    }
    
    @property(nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property(nonatomic, copy) NSString *title;
    @property(nonatomic, copy) NSString *subtitle;
    
    @end
    

    Annotation.m

    #import "Annotation.h"
    
    @implementation Annotation
    @synthesize coordinate, title, subtitle;
    
    @end
    

    Step 4: Add an annotation you will have your custom image!

    MapViewController.m

    MKCoordinateRegion Bridge = { {0.0, 0.0} , {0.0, 0.0} };
            Bridge.center.latitude = [[[testArr objectAtIndex:updates] objectAtIndex:1] floatValue];
            Bridge.center.longitude = [[[testArr objectAtIndex:updates] objectAtIndex:0] floatValue];
            Bridge.span.longitudeDelta = 0.01f;
            Bridge.span.latitudeDelta = 0.01f;
    
            Annotation *ann = [[Annotation alloc] init];
            ann.title = @"I'm a pin";
            ann.subtitle = @"Your subtitle";
            ann.coordinate = Bridge.center;
            [mapView addAnnotation:ann];
    

    I hope this helps!