3

I have an application that involves lots of MKOverlayViews. Every time one is added to the map, it is also added to an NSArray. When the user wishes to stop adding overlays, I would like for them to be able to save them, and be able to access them later. How can I store an array of MKOverlayViews into a plist, and then reload them back into a mapView later? Is this possible?

I'm attempting to use this code to take the MKPolylineViews from the array and add the corresponding MKPolylines to the map, but it crashes at the '[mapView addOverlay....' line.

Writing array to plist:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(overlays)
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:overlays];
    [defaults setObject:data forKey:@"theKey"];
    [defaults synchronize];
}

Reading data from plist:

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

for(MKPolylineView* a in arr)
    [mapView addOverlay:a.polyline];
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92

1 Answers1

1

I'd look at archiving with NSArchiver or NSKeyedArchiver and then writing them out to disk. Probably better than trying to squeeze them into a plist somehow...

isaac
  • 4,867
  • 1
  • 21
  • 31
  • I tried that, but I encountered some problems. I have to change the MKPolylines on the map into MKPolylineViews so that they are NSCoding compliant. Once I decode them, I have difficulty changing them back into MKPolylines that I can put on the map. Although I access the polyline property of each MKPolylineView, the compiler tells me that it is nil, and my program crashes. Not sure why this happens. – eric.mitchell Oct 08 '11 at 23:26
  • Do you have a code sample? I still think my suggested approach is a reasonable one, it may just be a matter of trying some different transformers to get your MKPolylines properly archived and retrieved... – isaac Oct 08 '11 at 23:31