在obj c中的折线上创建方向箭头(箭头)

时间:2016-08-10 18:08:04

标签: ios objective-c iphone google-maps

我一直在寻找相当多的但是找不到任何答案,无论如何,我正在使用Obj C为$ rn-ios for iOS工作并使用多个绘制路线(Google Maps)服务器以数组的形式提供给我的坐标。但问题是我想在该行上显示polyline,以便可以在地图上看到方向。请帮忙。

1 个答案:

答案 0 :(得分:0)

这是一个绘制漂亮的小箭头线的功能。它有一些你可以调整的参数:

void TRDrawLineWithArrow(CGContextRef CXT, CGPoint FROMPOINT, CGPoint TOPOINT, CGFloat WIDTH, CGFloat ARROWSIZEMULTIPLE)
{
    CGFloat         rise = TOPOINT.y - FROMPOINT.y;
    CGFloat         run = TOPOINT.x - FROMPOINT.x;

    // trig
    CGFloat         length = sqrt(rise*rise + run+run);
    CGFloat         angle = atan2(rise, run);

    // the length of our arrowhead
    CGFloat         arrowLen = WIDTH*ARROWSIZEMULTIPLE;

    // push graphics context
    CGContextSaveGState(CXT);

    // transform context according to line's origin and angle
    CGContextTranslateCTM(CXT, FROMPOINT.x, FROMPOINT.y);
    CGContextRotateCTM(CXT, angle);

    // draw straight line
    CGContextMoveToPoint(CXT, 0, -WIDTH/2.);
    CGContextAddLineToPoint(CXT, 0, WIDTH/2.);
    CGContextAddLineToPoint(CXT, length-arrowLen, WIDTH/2.);

    // draw arrowhead
    CGContextAddLineToPoint(CXT, length-arrowLen, (WIDTH*ARROWSIZEMULTIPLE)/2.);
    CGContextAddLineToPoint(CXT, length, 0);
    CGContextAddLineToPoint(CXT, length-arrowLen, -(WIDTH*ARROWSIZEMULTIPLE)/2.);

    CGContextAddLineToPoint(CXT, length-arrowLen, -WIDTH/2.);
    CGContextAddLineToPoint(CXT, 0, -WIDTH/2.);

    // fill the path
    CGContextFillPath(CXT);

    // pop graphics context
    CGContextRestoreGState(CXT);
}

你可以从这样的UIView中调用它:

CGContextRef cxt = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(cxt, [UIColor blackColor].CGColor);

TRDrawLineWithArrow(cxt, CGPointMake(10,10), CGPointMake(300,100), 5, 3);