9

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

iDroid
  • 1,140
  • 1
  • 13
  • 30

2 Answers2

21

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);
sch
  • 27,436
  • 3
  • 68
  • 83
  • Yeah Thanks; But unfortunately my array has the objects of a class(which has longitude and latitude) , So i did created another array for convert the coordinates to points, hence i found this is going bit complex, So i moved for creating poly line , where i input my coordinates array, and trying to create the poly line View. – iDroid Feb 16 '12 at 12:42
2

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);
Ramis
  • 13,985
  • 7
  • 81
  • 100