1

So far I have been very impressed with Objective-C and the Cocoa Frameworks but I have to say that I am a little confused by the best way to create an array of CLLocationCoordinate2D to pass to the polylineWithCoordinates:count: I am pretty sure that this is how I need to setup the array.

NSUInteger numPoints = [locationData count]; 
CLLocationCoordinate2D *arrayPtr = malloc(numPoints * sizeof(CLLocationCoordinate2D));

But I am not sure how to access each location, I thought I could use arrayPtr[i] but I can't seem to get that to work properly.

for(NSUInteger counter=0; counter<numPoints; counter++) {
    arrayPtr[counter] = [[locationData objectAtIndex:counter] coordinate];
}

EDIT:

I have it working now, I was viewing the memory wrong in the debugger, but is there another way (maybe without malloc) I did try encoding the values in an NSArray using NSValue, only to find that I need a c-type array instead :( any alternative methods would be most welcome.

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

1 Answers1

1

That should be valid - what error are you getting?

A better approach might be to use CFArray instead of manually managing the malloc.

Bill Dudney
  • 3,358
  • 1
  • 16
  • 13
  • Hum I know it's an old question, but could you please complete your answer by telling us more about the CFArray class ? Even after have imported the corefoundation library, I still got the _Unknown type CFArray, do you mean NSArray ?_ when I declare `CFArray *myArray;`. Thanks. – Lucien May 16 '13 at 12:23
  • The documentation is way better than the 2 or 3 lines I've got time to type here. Read them, it will help. – Bill Dudney May 24 '13 at 21:53