如何从点数组创建CGPathRef

iDr*_*oid 8 objective-c mapkit ios5

大家好我一直在地图应用程序中工作,我需要在两个位置之间绘制路线,我也有路线坐标(使用谷歌方向api)并将其保存在一个数组中,现在我需要做的就是从点数组创建路径,稍后我将使用MKOverlayPathView的路径在地图上创建实际路线.这里我的问题是如何从坐标数组创建一个CGPathRef,或任何其他方式来做同样的操作
在此先感谢

sch*_*sch 21

假设坐标作为NSValue对象存储在NSArray中,您可以执行以下操作:

CGMutablePathRef path = CGPathCreateMutable();
if (points && points.count > 0) {
    CGPoint p = [(NSValue *)[points objectAtIndex:0] CGPointValue];
    CGPathMoveToPoint(path, nil, p.x, p.y);
    for (int i = 1; i < points.count; i++) {
        p = [(NSValue *)[points objectAtIndex:i] CGPointValue];
        CGPathAddLineToPoint(path, nil, p.x, p.y);
    }
}
// do stuff
CGPathRelease(path);
Run Code Online (Sandbox Code Playgroud)