Custom image for MKAnnotation
Asked Answered
D

3

10

I have created an annotation which I'm adding to an MKMapView. How would I go about having a custom image instead of the standard red pin?

@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end
Durrace answered 18/9, 2011 at 12:18 Comment(0)
L
18

MKMapView gets its pin views from its delegate method mapView:viewForAnnotation: So you have to:

  1. Set your view controller as the map's delegate.
  2. Implement mapView:viewForAnnotation: in your controller.

Set your controller as delegate

@interface MapViewController : UIViewController <MKMapViewDelegate>

Mark the interface with the delegate protocol. This let's you set the controller as MKMapView's delegate in Interface Builder (IB). Open the .xib file containing your map, right click the MKMapView, and drag the delegate outlet to your controller.
If you prefer to use code instead IB, add self.yourMapView.delegate=self; in the controller's viewDidLoad method. The result will be the same.

Implement mapView:viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // this part is boilerplate code used to create or reuse a pin annotation
    static NSString *viewId = @"MKPinAnnotationView";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*) 
        [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
    if (annotationView == nil) {
        annotationView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:viewId] autorelease];
    }
    // set your custom image
    annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"];
    return annotationView;
}
Lintel answered 18/9, 2011 at 13:7 Comment(0)
N
1

Override the mapView:viewForAnnotation: delegate method. If the annotation param points to one of your custom annotations, return a custom view that looks to your liking.

Nutpick answered 18/9, 2011 at 12:22 Comment(1)
Can you point me to an example of how to do this? I'm new to objective c. ThanksDurrace
T
0

To set custom image instead of standart MKPinAnnotationView the only way to do that is to use MKAnnotationView with function - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation. Here's the example:

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

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

     static NSString *identifier = @"Annotation";

     MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

     if (!aView) {
          aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
          aView.image = [UIImage imageNamed:@"Untitled1.png"];
          aView.canShowCallout = YES;
          aView.draggable = YES;
     } else {
          aView.annotation = annotation;
     }

     return pin;
}

For the aView.image value You can set Your own image. And also look into MKAnnotationView class reference to handle better with it.

Turney answered 15/6, 2016 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.