我想问一个关于核心位置和核心数据的问题。我看了一些问题,但无法做到这一点.. 我有一个应用程序,它存储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]]];
}
}
在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 :(得分:0)
如果调用stopUpdatingLocation
并未停止位置更新,则很可能self->locationManager
为零。这意味着你并没有真正打电话。
很难确定为什么会发生这种情况,除了你的代码似乎使用@property
声明隐含的任何语义来强调不。只需在location
中分配viewDidLoad
即可避免任何声明,并使用self->locationManager
查找经理。假设location
是一个属性,您应该将其分配给self.locationManager
,并在查找时使用它。
答案 1 :(得分: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];
}
}
答案 2 :(得分:0)
在你的didUpdateToLocation中执行此代码
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = - [newLocation.timestamp timeIntervalSinceNow]; if(locationAge&gt; 5)返回;
//忽略缓存位置,我们想要当前的位置
if(newLocation.horizontalAccuracy&lt; = 0)返回; //忽略无效
//等待GPS准确度(将<400) if(newLocation.horizontalAccuracy&lt; 400){ _longtitudeLabel.text = [NSString stringWithFormat:@“%。8f”,newLocation.coordinate.longitude]; _latitudeLabel.text = [NSString stringWithFormat:@“%。8f”,newLocation.coordinate.latitude]; [经理stopUpdatingLocation];
//将nil分配给locationManager对象并委托
locationManager.delegate = nil;
locationManager = nil;
}
}
感谢。