【问题标题】:Storing CLLocation (latitude, longtitude data) in to Core Data将 CLLocation(纬度、经度数据)存储到核心数据中
【发布时间】:2013-04-27 21:34:09
【问题描述】:

我想问一个关于核心位置和核心数据的问题。我看了一些问题,但不能这样做.. 我有一个应用程序,它在 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次,然后存储..

谢谢!

【问题讨论】:

  • 请解释:“代码开始计算纬度和经度数据”以及这与回调“locationManager:didUpdateToLocation:fromLocation:”有何关系。
  • 我只需要,虽然我猜..当用户打开这个应用程序时,这段代码正在工作.. - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation];我需要调用一次......然后我可以存储它

标签: core-data core-location locationmanager cllocation


【解决方案1】:

在您的 didUpdateToLocation 中执行此代码

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

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

    // 忽略缓存位置,我们想要当前位置
    if (newLocation.horizo​​ntalAccuracy

    // 等待 GPS 精度(将

    //将 nil 分配给 locationManager 对象和委托
    locationManager.delegate = nil;
    locationManager = nil;

    }

}

谢谢。

【讨论】:

    【解决方案2】:

    几件事:

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

    【讨论】:

      【解决方案3】:

      如果调用stopUpdatingLocation 不会停止位置更新,那么self-&gt;locationManager 很可能为零。那意味着你并没有真正在打电话。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-25
        • 1970-01-01
        • 2012-03-22
        • 2011-09-18
        • 1970-01-01
        • 2012-12-01
        相关资源
        最近更新 更多