5

オーバーレイ ビューで 2 点間に直線を引こうとしています。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();
}

ご協力ありがとうございました。

4

1 に答える 1

3

startP値であるandendPを使用して線が描画されていますが、値を含む whichCGPointを使用して初期化されています。theMapRectMKMapPoint

代わりに、theRect変換元をtheMapRect使用して初期化します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

于 2012-05-23T02:17:48.560 に答える