我有一个应用程序,它创建一个包含(其中包括)某些位置数据的类实例。
在app delegate中,我设置了位置服务并开始抓取位置数据;
//Delegate method to receive location infomation from locationManager
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
latestLocation = newLocation;//Make latest location the same as NewLocation
NSLog(@"Location is: %@", latestLocation);
}
我将最新位置声明为属性,以便我可以从另一个类获取CLLocation实例。
我的捕获类,当调用其init方法时调用CLLocation;
//Designated initialiser
-(id) initWithVideoPath:(NSString *) vPath
userNotes:(NSString *) uNotes
retentionState:(NSString *) rState
{
//Call the super classes designated initializer
[super init];
//Get a pointer to the application delegate so we can access the location props
Rolling_VideoAppDelegate *appDelegate = (Rolling_VideoAppDelegate*)[UIApplication sharedApplication].delegate;
//If superclass failed to init
if (!self)
return nil;
//Give the variables some initial values
[self setVideoPath:vPath];
[self setUserNotes:uNotes];
[self setRetentionState:rState];
dateCreated = [[NSDate alloc] init];
mp = [[MapPoint alloc]initWithCoordinate:[[appDelegate latestLocation]coordinate]];//get the location from the coords from appDelegate
return self;
[dateCreated release];
}
然而,当调用mapPoint init时,应用程序崩溃。问题是我没有正确获取CLLocation信息。
任何人都可以帮助我。
提前致谢,
富
答案 0 :(得分:0)
我仍然不确定为什么原始解决方案不起作用,所以如果有人有任何见解,请指教。
然而,我已经解决了使用NSUserDefaults 的一项略显不雅的工作 latestLocation = newLocation;//Make latest location the same as NewLocation
//Use NSUser Defaults to save the CLLocation instance
NSUserDefaults *location = [NSUserDefaults standardUserDefaults];
[location setDouble:latestLocation.coordinate.latitude forKey:@"lat"];
[location setDouble:latestLocation.coordinate.longitude forKey:@"longd"];
我需要打破lat,只要NSUserDefaults不存储CLLocation对象(NSCoding兼容性),就可以在capture类中重构它们;
NSUserDefaults *location = [NSUserDefaults standardUserDefaults];//Get a handle to the user defaults
CLLocationDegrees lat = [location doubleForKey:@"lat"];
CLLocationDegrees longd = [location doubleForKey:@"longd"];
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:lat longitude:longd];
mp = [[MapPoint alloc]initWithCoordinate:[currentLocation coordinate]];//get the location from the coords from appDelegate