用户退出应用程序时在MapKit中存储注释

时间:2010-12-22 19:23:02

标签: iphone mapkit store mkannotation autosave

我希望在退出应用程序时存储我的注释,并在启动应用程序时再次弹出....我怎样才能实现这一点?有什么想法吗?

-(IBAction)storeLocation:(id)sender{

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

请...需要一些帮助!
谢谢!

1 个答案:

答案 0 :(得分:2)

如果您只想保存一个注释的坐标,最简单的方法是使用NSUserDefaults

保存:

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];

加载(例如在viewDidLoad中):

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