Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can ;) To do so, you need to implement this method :</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation </code></pre> <p>In this one you will have to create a instance of a subclass of MKAnnotationView only for the user location case of annotation like:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { if (userView == nil) { userView=[[[UserLocationView alloc] initWithAnnotation:annotation reuseIdentifier:@"user"] autorelease]; } [self addObserver:userView forKeyPath:@"userHeading" options:NSKeyValueObservingOptionNew context:NULL] ; return userView ; } return nil; } </code></pre> <p>Note the KVO statement to let your custom MKAnnotationView that the value has changed in your MKAnnotationView subclass you need to override drawRect; like that for example</p> <pre><code>- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); NSAssert(context !=nil, @"Context was nil") ; CGContextSetRGBStrokeColor (context,1.0f,0.0f,0.0f,1.0f) ; CGContextSetLineWidth(context, 2.0f); CGContextMoveToPoint(context, 0, 15); CGContextAddLineToPoint(context, 30, 15); CGContextMoveToPoint(context, 15,0); CGContextAddLineToPoint(context, 15, 30); CGContextStrokePath(context); CGContextSetLineWidth(context, 4.0f); CGContextAddArc (context,15,15,13,0,6.28,0); CGContextStrokePath(context); hdg = radians([self heading]); dx = self.frame.size.width / 2 ; dy = self.frame.size.height/ 2 ; if (hdg &lt; M_PI) { y = (cos(hdg) * dy ) - dy ; x = (sin(hdg) * dx ) + dx ; } else { y = (cos(hdg) * dy ) + dy ; x = (sin(hdg) * dx ) + dx ; } // NSLog(@"Heading:%3f Hdg:%3f X:%3f Y:%3f", heading, hdg, x, y) ; CGContextSetRGBStrokeColor (context,1.0f,0.0f,1.0f,1.0f) ; CGContextSetLineWidth(context, 2.0f); CGContextMoveToPoint(context, dx,dy); CGContextAddLineToPoint(context, x, y); CGContextStrokePath(context); } </code></pre> <p>This draws a red reticle with a purple line showing the heading (in my sample the course is bugged because i am just weak in trigonometry ;) but it draws :) </p> <p>You of course need to handle the KVO notification like:</p> <pre><code>-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { [self setHeading: [[change objectForKey:NSKeyValueChangeNewKey]doubleValue]] ; [self setNeedsDisplay] ; NSLog(@"Heading is %f", [self heading]); } </code></pre> <p>the <code>-setNeedsDisplay</code> will trigger the redraw.</p> <p>Make sure that you custom view has a non-zero rect frame, and do NOT set an image to it.</p>
 

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