4

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:


- (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!

Till
  • 27,559
  • 13
  • 88
  • 122
JakesRassie
  • 616
  • 1
  • 6
  • 20
  • 1
    The method above is a delegate method for the MapView. The reason you keep getting that answer is because that is how you setup a custom Image for a pin. It is called once only if you have added a mapview and setup the delegate. I recommend you follow a tutorial like this http://www.techotopia.com/index.php/Working_with_Maps_on_the_iPhone_with_MapKit_and_the_MKMapView_Class. This will help you understand how MapView and delegate methods work – AdamM Apr 11 '13 at 14:28
  • Rephrasing Adam's answer a bit, your MapView's delegate needs to implement that method. The view controller is usually used as a MapView's delegate. – Brendon Apr 11 '13 at 14:51
  • I am still stuggling, I am pretty new to iOS but have a pretty good understanding as to what needs to be done in terms of implementing methods, but implementing a method which acts as a delegate doesn't seem as simple as I'd thought it would be. Do you know of any step-by-step tutorials, or could you give me an example as to how I would do this?? – JakesRassie Apr 12 '13 at 05:43

1 Answers1

18

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!

JakesRassie
  • 616
  • 1
  • 6
  • 20
  • 1
    I really appreciate your answer. I went through the same pain as I was not able to find a perfect example on this. Your code helped me a lot. – GenieWanted Nov 15 '13 at 12:24
  • 2
    This code is "worky" but it's not perfect. At first you should dequeue your annotation view with an reusable identifier so the view for that annotation (type) is not allocated all the time for all the pins. This is not an issue for low number of annotations but if you have complex views with many annotations, your performance could suffer either. Cheers – cybercow May 13 '14 at 19:30