1

我想问一个关于核心位置和核心数据的问题。我看了一些问题,但做不到。我有一个应用程序,它在 UITableView 中存储一些文本字段、照片、日期和时间数据 使用核心数据,我存储了所有内容(照片、文本、日期等)。现在尝试存储位置数据我做不到。

这是我的一些代码。

    #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]]];
    }
}

在保存按钮中

    - (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 次,并存储..

谢谢!

4

3 回答 3

0

在您的 didUpdateToLocation 中执行此代码

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5) 返回;

    // 忽略缓存位置,我们想要当前位置
    if (newLocation.horizo​​ntalAccuracy <= 0) return; // 忽略无效

    // 等待 GPS 精度(将 < 400) if (newLocation.horizo​​ntalAccuracy < 400) { _longtitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude]; _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude]; [经理停止更新位置];

    //将 nil 分配给 locationManager 对象并委托
    locationManager.delegate = nil;
    位置管理器 = 无;

    }

}

谢谢。

于 2013-10-25T07:12:36.367 回答
0

如果调用stopUpdatingLocation不会停止位置更新,那么很可能self->locationManager是 nil。这意味着你并没有真正打电话。

很难确切地确定为什么会发生这种情况,除了您的代码似乎强调使用@property声明所暗示的任何语义。只需分配 to locationin即可viewDidLoad避免任何声明,并且使用 dos 查找经理self->locationManager也是如此。假设这location是一个属性,您应该将其分配给self.locationManager,并在查找时使用它。

于 2013-05-03T16:13:01.263 回答
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];
    }    
}
于 2013-05-03T16:31:39.620 回答