在叠加视图上未绘制线条

in *_*eps 5 iphone cgcontext mapkit mkmapview mkoverlay

我试图在叠加视图中的两点之间绘制一条直线.在MKOverlayView方法中,我认为我做得正确,但我不明白为什么它没有绘制任何行...

有谁知道为什么?

- (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();
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

小智 3

startP该线是使用and endPwhich is值绘制的CGPoint,但它们是使用theMapRectwhich containsMKMapPoint值进行初始化的。

相反,请使用theRect您从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));
Run Code Online (Sandbox Code Playgroud)


MKOverlayView最后,为什么不使用 a来MKPolylineView避免手动绘制线条,而不是使用 custom 呢?