Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create CGPathRef from Array of points

Hi all i've been working in a map application, Where i needed to draw the route between two locations, I've got route coordinates too (using google Direction api) and kept it in an array, Now all i need to do is creating a path from the array of points, later i will use the path with MKOverlayPathView for creating real routes on the map. Here my problem is how to create a CGPathRef from the array of coordinates, Or any other way to do the same operation
Thanks in Advance

like image 588
iDroid Avatar asked Feb 16 '12 10:02

iDroid


2 Answers

Assuming the coordinates are stored in an NSArray as NSValue objects, you can do the following:

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);
like image 93
sch Avatar answered Dec 03 '22 06:12

sch


Another way to create path:

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);
like image 31
Ramis Avatar answered Dec 03 '22 06:12

Ramis