【问题标题】:How to create CGPathRef from Array of points如何从点数组创建 CGPathRef
【发布时间】:2012-03-07 18:30:51
【问题描述】:

大家好,我一直在使用地图应用程序,我需要在两个位置之间绘制路线,我也有路线坐标(使用 google Direction api)并将其保存在一个数组中,现在我只需要要做的是从点数组创建路径,稍后我将使用带有 MKOverlayPathView 的路径在地图上创建真实路线。这里我的问题是如何从坐标数组创建一个 CGPathRef,或者任何其他方式来执行相同的操作
提前致谢

【问题讨论】:

    标签: objective-c ios5 mapkit


    【解决方案1】:

    假设坐标作为 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);
    

    【讨论】:

    • 是的,谢谢;但不幸的是,我的数组有一个类的对象(有经度和纬度),所以我确实创建了另一个数组来将坐标转换为点,因此我发现这有点复杂,所以我开始创建折线,我在哪里输入我的坐标数组,并尝试创建折线视图。
    【解决方案2】:

    另一种创建路径的方法:

    CGPoint points[5];
    // TODO: Fill points array with values.
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddLines(path, NULL, points, 5);
    // TODO: Do something useful with path.
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多