1

My app track the route taken by a user and then creates a MKPolyline which updates every time the users location changes to show the route taken.

My next step is to store the route taken and save it so that The user can view the route the taken later (similar to runkeeper and other GPS tracking apps).

I've been struggling with this for a few days. First I started looked into storing an array of the locations into NSUserDefaults. To do this I was first converting the individual array objects into an NSValue, but quickly found out that NSValues was not a suitable format to store in NSUserDefaults. I think I'm right in saying that I could potentially store the coordinate values into NSData to be stored in NSUserDefaults but I'm not quite sure how this is done.

After more research I think I have settled on storing the coordinates into Core Data. But I have a few questions on doing this as this the first time I have tried to use Core Data. Would I store the coordinates as individual attributes of Latitude and longitude double values? or can I store them as an array? Then I'm also unsure as to how I would recreate CLLocationCoordinate2D to then rebuild the MKpolyline.

Sorry for the barrage of questions - I've been stuck on this for a while now and its driving me slightly stir crazy!!

Thanks

Khledon
  • 193
  • 5
  • 23

1 Answers1

0

When creating an MKPolyline from an array of CLLocationCoordinate2D, you'll be using a C-style array, so using this method

+ (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count

Implies that somewhere you have a declaration like this:

CLLocationCoordinate2D coords[SIZE];

You can convert C-style arrays to and from NSData directly, without bothering with NSArray or NSValue:

NSData *coordsData = [NSData dataWithBytes:coords length:SIZE*sizeof(CLLocationCoordinate2D)]
....
CLLocationCoordinate2D *coords = [coordsData bytes];

This is probably the easiest way-- convert your array to NSData to save it, convert back when reading it. Once you have the coordinate array, you can re-create your MKPolyLine.

Whether you use NSUserDefaults or Core Data doesn't really matter as far as the data encoding goes, because NSData works equally well with either. The decision depends on how much data your app will have and on how you'll be using that data.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170