Lines are not displayed in the overlay view

I am trying to draw a straight line between two points in an overlay view. In the MKOverlayView method, I think I'm doing it right, but I don’t understand why it does not draw any lines ...

Does anyone know why?

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{
    UIGraphicsPushContext(context);

    MKMapRect theMapRect = [[self overlay] boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    // Clip the context to the bounding rectangle.
    CGContextAddRect(context, theRect);
    CGContextClip(context);

    CGPoint startP = {theMapRect.origin.x, theMapRect.origin.y};
    CGPoint endP = {theMapRect.origin.x + theMapRect.size.width,
        theMapRect.origin.y + theMapRect.size.height};

    CGContextSetLineWidth(context, 3.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, startP.x, startP.y);
    CGContextAddLineToPoint(context, endP.x, endP.y);
    CGContextStrokePath(context);

    UIGraphicsPopContext();
}

Thank you for your help.

+5
source share
1 answer

A string is drawn using startPand endP, which are values CGPoint, but they are initialized using a theMapRectthat contains values MKMapPoint.

Instead, initialize them using the theRectone you convert from theMapRectusing rectForMapRect.

, MKRoadWidthAtZoomScale. 3.0 , .

:

CGPoint startP = {theRect.origin.x, theRect.origin.y};
CGPoint endP = {theRect.origin.x + theRect.size.width,
    theRect.origin.y + theRect.size.height};

CGContextSetLineWidth(context, 3.0 * MKRoadWidthAtZoomScale(zoomScale));


, MKOverlayView, MKPolylineView, ?

+3

All Articles