Thursday 15 December 2016

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


}





  • (void)stopLocationManager{
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
    }



Now, I have the below questions.




  1. 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?



  2. 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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...