問題描述
用戶退出應用程序時在 MapKit 中存儲註釋 (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!
‑‑‑‑‑
參考解法
方法 1:
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...
}
(by Krismutt、user467105)