2013-05-03 86 views
1

我想問一個關於核心位置和核心數據的問題。我看了一些問題,但無法做到這一點.. 我有一個應用程序,它存儲一些文本域,照片,日期和時間數據在UITableView與核心數據,我存儲的一切(照片,文本,日期等)現在試圖存儲位置數據我無法做到。將CLLocation(緯度,經度數據)存儲到Core Data中

這是我的一些代碼在這裏。

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 

    NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init]; 
    [myFormatter setDateFormat:@"MM-dd-yyyy HH:mm"]; 
    [myFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 

    todaysDate = [myFormatter stringFromDate:[NSDate date]]; 
    myDateLabel.text = todaysDate; 

    UIView *patternBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
    patternBg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background01.png"]]; 
    self.tableView.backgroundView = patternBg; 

    // If we are editing an existing picture, then put the details from Core Data into the text fields for displaying 

    if (currentPicture) 
    { 
     [companyNameField setText:[currentPicture companyName]]; 
     [myDateLabel setText:[currentPicture currentDate]]; 

     if ([currentPicture photo]) 
      [imageField setImage:[UIImage imageWithData:[currentPicture photo]]]; 
    } 
} 
在saveButton

- (IBAction)editSaveButtonPressed:(id)sender 
{ 
    // For both new and existing pictures, fill in the details from the form 
    [self.currentPicture setCompanyName:[companyNameField text]]; 
    [self.currentPicture setCurrentDate:[myDateLabel text]]; 
    [self.currentPicture setCurrentTime:[myTimeLabel text]]; 
    [self.currentPicture setLatitudeData:[_latitudeLabel text]]; 
    [self.currentPicture setLongtidueData:[_longtitudeLabel text]]; 

} 

和最後一個,我的LocationManager的方法

..

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     _longtitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
     [self->locationManager stopUpdatingLocation]; 
    } 


} 

我試圖 「[的LocationManager stopUpdatingLocation]。」很多時候,但是當我進入應用程序時,代碼開始計算緯度和經度數據,我只想將該數據記錄下來1次,並存儲。

謝謝!

+0

請解釋:'代碼開始計算緯度和經度數據'以及它如何與回調'locationManager:didUpdateToLocation:fromLocation:'相關。 – Wain 2013-05-03 15:40:52

+0

我只是需要做,而我猜..當用戶打開此應用程序,此代碼正在工作.. - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; 我需要調用這一次..然後我可以存儲它 – serkos 2013-05-09 11:17:28

回答

0

如果調用stopUpdatingLocation不停止位置更新,那麼最有可能的self->locationManager爲零。那意味着你並沒有真的打過電話。

很難確定到底爲什麼會發生這種事,除非你的代碼似乎使使用由@property聲明暗示的任何語義的點。只需在viewDidLoad中指定location即可避免任何聲明,並且使用self->locationManager查找經理也可以。假設location是一個屬性,您應該將其分配給self.locationManager,並在查找它時使用它。

0

有兩件事情:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5) return; // ignore cached location, we want current loc 

    if (newLocation.horizontalAccuracy <= 0) return; // ignore invalid 

    // wait for GPS accuracy (will be < 400) 
    if (newLocation.horizontalAccuracy < 400) { 
     _longtitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude]; 
     _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude]; 
     [manager stopUpdatingLocation]; 
    }  
} 
0

在你didUpdateToLocation爲此代碼

  • (無效)的LocationManager:(CLLocationManager *)經理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {0} {0} {0} NSTimeInterval locationAge = - [newLocation.timestamp timeIntervalSinceNow]; if(locationAge> 5)return;

    //忽略緩存位置,我們希望當前位置
    if(newLocation.horizo​​ntalAccuracy < = 0)return; //忽略無效

    //等待GPS精確度(將< 400) 如果(newLocation.horizo​​ntalAccuracy < 400){ _longtitudeLabel.text = [NSString的stringWithFormat:@ 「%8F。」,newLocation.coordinate。經度];_latitudeLabel.text = [NSString stringWithFormat:@「%。8f」,newLocation.coordinate.latitude]; [manager stopUpdatingLocation];

    //將位置管理器對象和委託 locationManager指定爲零。delegate = nil;
    locationManager = nil;

    }

}

感謝。