我試圖爲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復位並開始更新位置時用戶刷新。但是,它沒有工作。我的錯是什麼?
對不起,我有這麼多問題。如果有人給我建議,我將不勝感激。
檢查文檔怎麼我認爲這是對的位置兩次連續調用之間的時間延遲。 –