Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving MKOverlayView array -> plist

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];
like image 941
eric.mitchell Avatar asked Sep 13 '26 02:09

eric.mitchell


1 Answers

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...

like image 139
isaac Avatar answered Sep 15 '26 23:09

isaac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!