在叠加视图上未绘制线条

时间:2012-05-22 20:09:18

标签: iphone mkmapview mapkit cgcontext 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();
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

正在使用startPendP绘制线条CGPoint,但这些线条使用包含theMapRect值的MKMapPoint进行初始化。

而是使用theRect使用theMapRectrectForMapRect转换来初始化它们。

此外,对于线宽,您可能希望使用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

相关问题