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の互換性)を格納しない限り、latを分割する必要があり、capturesクラスでそれらを再構築します。

    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 に答える