0

我有一个应用程序,它创建一个包含(除其他外)一些位置数据的类实例。

在应用程序委托中,我设置了位置服务并开始获取位置数据;

//Delegate method to receive location information 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 信息。

4

1 回答 1

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

我需要打破纬度,只要 NSUserDefaults 不会存储 CLLocation 对象(NSCoding 兼容性),在捕获类中重建它们;

    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
于 2010-12-28T16:23:14.960 回答