Search code examples
iphonemapkitstoremkannotationautosave

Store annotations in MapKit when user quit application


I want my annotation to be stored when I quit the application and pop up again when I launch the application.... How can I make this possible? Any ideas?

-(IBAction)storeLocation:(id)sender{

    SparaPosition *position=[[SparaPosition alloc] initWithCoordinate:location];
    [mapView addAnnotation:position];
    savedPosition = location;

Please... need some help!
Thanks!


Solution

  • If you want to save just one annotation's coordinates, simplest way is to use NSUserDefaults.

    To save:

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    [ud setDouble:location.latitude forKey:@"savedCoordinate-latitude"];
    [ud setDouble:location.longitude forKey:@"savedCoordinate-longitude"];
    [ud setBool:YES forKey:@"savedCoordinate-exists"];
    [ud synchronize];
    

    To load (in viewDidLoad for example):

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    if ([ud boolForKey:@"savedCoordinate-exists"])
    {
        CLLocationCoordinate2D savedCoordinate;
        savedCoordinate.latitude = [ud doubleForKey:@"savedCoordinate-latitude"];
        savedCoordinate.longitude = [ud doubleForKey:@"savedCoordinate-longitude"];
        //create annotation object using savedCoordinate and add to map view...
    }