CLLocationManager didn't updated data properly
I've got the problem when I tried to experiment for LBS app. When tested in simulator, it seems to work. Then, I tried it in iPhone. It didn't work as expected.
When I start the app, it displayed all information such as my location latitude, longitude, distance, etc.However, those information haven't changed while I walked away. Either run app or my iPhone automatic shut up (then open app again), those data still unchanged.
While I switch off the app and restart it, it can display an update information.
Here is my code
I set up CLLocationManager in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
//setup locationManager
locationManager =[[CLLocationManager alloc]init];
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
Here is code for CLLocationManager Delegate
-(void)locationManager:(CLLocationManager *)manager 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];
}
}
}
Now, I have the below questions.
What's wrong on my setting of CLLocationManager? Should I put below in appDelegate (under didFinishLaunchingWithOptions method) let it run it background? Will it drain battery power quickly? Or set it in ViewWillAppear instead of ViewDidLoad?
On the other hand, I put 'pull down to refresh' into program. I thought that updated data can be updated during refresh it. However, it won't work as well. Here is my code of refresh (I put EGOTableViewPullRefresh for refresh function).
(void)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];
}
What's my thought is CLLocationManager reset and start update location during users refresh it. However, it didn't work. What is my fault?
I'm sorry,I have so many questions. I would appreciate if someone give me advise.
You cannot control when location updates are provided, you can only start the CLLocationManager
and the OS will decide when it provides your CLLocationManagerDelegate
with updates.
If you want it to force an update, you can try checking if the CLLocationManager
is already started and if it is, stop it and restart.
By the way, as a side note, I recommend you wrap the CLLocationManager in a singleton class. If you keep starting the location manager it will run in many instances with the application and drain the battery (I can't see in your code when you actually stop it). Start and stop CLLocationManager
as you need it.
I am not clear what, exactly, you want. If you want to just display the location when you open the app, move the startupdatinglocation line to viewDidAppear. Display an activity indicator on screen as it will take a moment to get homed in and update.
If you want it to continually update, do not call stopUpdatingLocation. It will fire the delegate method whenever your location changes to meet the settings of the manager.
Another thing I noticed, did you mean to say
if (locationAge < 5.0) return;
Seems you say great than 5.0 in which case, if it takes longer than that to get the update, you will never respond to another.
Instead of setting up your manager repeatedly, create a singleton or save in a property. Once it is initiated, you should only have to start/stop updates as needed.
You may benefit from some of my tips here:
TTLocationHandler GitHub Repository
链接地址: http://www.djcxy.com/p/20348.html