Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a case where I wanted something like a standard Pin annotation, but the designer wanted a custom graphic.</p> <p>I wrote a subclass of <code>MKAnnotationView</code> to display the graphic. The only difference is that it overrides the standard class's <code>image</code>.</p> <h1>BlipAnnotationView.h</h1> <pre><code>#import &lt;MapKit/MapKit.h&gt; @interface BlipAnnotationView : MKAnnotationView - (id)initWithAnnotation:(id&lt;MKAnnotation&gt;)annotation reuseIdentifier:(NSString *)reuseIdentifier; @end </code></pre> <h1>BlipAnnotationView.m</h1> <pre><code>#import "BlipAnnotationView.h" @implementation BlipAnnotationView - (id)initWithAnnotation:(id&lt;MKAnnotation&gt;)annotation reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; if (self) { UIImage *blipImage = [UIImage imageNamed:@"blip.png"]; CGRect frame = [self frame]; frame.size = [blipImage size]; [self setFrame:frame]; [self setCenterOffset:CGPointMake(0.0, -7.0)]; [self setImage:blipImage]; } return self; } @end </code></pre> <p>Then in the class that displays the map, I made the class implement the <code>MKMapViewDelegate</code> protocol. The <code>mapView:viewForAnnotation:</code> method creates a new instance of <code>BlipAnnotationView</code> if necessary.</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)annotation { NSLog(@"mapView:%@ viewForAnnotation:%@", mapView, annotation); static NSString *const kAnnotationIdentifier = @"BlipMapAnnotation"; BlipAnnotationView *annotationView = (BlipAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationIdentifier]; if (! annotationView) { annotationView = [[BlipAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationIdentifier]; } [annotationView setAnnotation:annotation]; return annotationView; } </code></pre> <p>Finally I set the class as the delegate of the map view in <code>awakeFromNib</code>:</p> <pre><code>- (void)awakeFromNib { [...] [_theMapView setDelegate:self]; } </code></pre> <p>I didn't have to change the code that positioned the annotation at all:</p> <pre><code>MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; [annotationPoint setCoordinate:[userLocation coordinate]]; [annotationPoint setTitle:label]; [_theMapView addAnnotation:annotationPoint]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload