我试图为LBS应用程序进行实验时遇到了问题。当在模拟器中测试时,它似乎工作。然后,我在iPhone上试了一下。它没有按预期工作。CLLocationManager没有正确更新数据
当我启动应用程序时,它会显示所有信息,例如我的位置纬度,经度,距离等等。但是,当我走开时,这些信息没有改变。无论是运行应用程序还是我的iPhone自动关闭(然后再次打开应用程序),这些数据仍然保持不变。
虽然我关闭应用程序并重新启动它,但它可以显示更新信息。
这里是我的代码
我成立了CLLocationManager在viewDidLoad中
- (void)viewDidLoad
{
[super viewDidLoad];
//setup locationManager
locationManager =[[CLLocationManager alloc]init];
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
这里是CLLocationManager代表
码 - (空)的LocationManager:(CLLocationManager *)经理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation.horizontalAccuracy < 0) return;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (myLocation == nil || myLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
self.myLocation = newLocation;
// NSLog(@"mylocation=%@",self.myLocation);
[self.mainTableView reloadData];
[self sortedDistArray];//this method is to calculate distance and put it in array. It work...
if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
[self stopLocationManager];
}
}
}
- (无效)stopLocationManager { [的LocationManager stopUpdatingLocation]; locationManager.delegate = nil; }
现在,我有以下问题。
我的CLLocationManager设置有什么问题?我应该在appDelegate下面(在didFinishLaunchingWithOptions方法下)让它运行它的背景吗?它会快速耗尽电池电量吗?或者将它设置在ViewWillAppear而不是ViewDidLoad中?
另一方面,我把'拉下来刷新'到程序中。我认为刷新它时可以更新更新的数据。但是,它不会起作用。这里是我的刷新代码(我把EGOTableViewPullRefresh用于刷新功能)。
(无效){reloadTableViewDataSource
// should be calling your tableviews data source model to reload // put here just for demo _reloading = YES; locationManager =[[CLLocationManager alloc]init]; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=kCLDistanceFilterNone; [locationManager setDelegate:self]; [locationManager startUpdatingLocation]; [self sortedDistArray]; [mainTableView reloadData]; [self doneLoadingTableViewData];
}
什么是我的想法是CLLocationManager复位并开始更新位置时用户刷新。但是,它没有工作。我的错是什么?
对不起,我有这么多问题。如果有人给我建议,我将不胜感激。
检查文档怎么我认为这是对的位置两次连续调用之间的时间延迟。 –