Kex
Kex

Reputation: 8579

CLLocation manager updating location twice

I am having a strange issue in my code. I want to call [locationManager startUpdatingLocation]; and when the update is done in - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation stop the updating immedietly with [locationManager stopUpdatingLocation]. I have placed this as the first line in the method. However on occasion it is getting called twice. Could anyone give me some pointers to why this might be happening? Doesn't make sense to me if the first thing I do is stop the update when I get the first one. Some code:

-(void)getLocation{

    [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];


}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [locationManager stopUpdatingLocation]; //kill it NOW or we have duplicates
    NSLog(@"didUpdateToLocation: %@", newLocation);

  //do other stuff....

}

I know it's duplicating because I will occasionally get the NSLog on screen twice. Any help would be really appreciated. Thanks!

Upvotes: 0

Views: 1046

Answers (2)

KennyHo
KennyHo

Reputation: 1142

To understand the reason why the callback (locationManager:didUpdateToLocation:fromLocation: or locationManager:didUpdateLocations:) is called twice (event more than twice), we should take a look at "behind the screen" of CLLocationManager where location data is acquired:

Calculating a phone’s location using just GPS satellite data can take
up to several minutes. iPhone can reduce this time to just **a few seconds**
by using Wi-Fi hotspot and cell tower data to quickly find GPS satellites.

Question1: Could anyone give me some pointers to why this might be happening?

Answer: CLLocationManager tries to provide you location as soon as possible so that you can process your logic without waiting for a few seconds. To do that, it caches location data from the last time you call [locationManager startUpdatingLocation].

To verify this point, you can try uninstalling your app, turn off master location service, then re-install your app, and launch it. You will see 2 things: (1)it takes a few seconds before calling back locationManager:didUpdateToLocation:fromLocation:, and (2) only one time invocation of locationManager:didUpdateToLocation:fromLocation:

Now, terminate your app, wait a bit, and re-launch it. You will see 2 different things: (1) the callback is called almost immediately(with cached data), and (2) the callback is called twice or more (depends on your waiting time and desired accuracy)

Question2: So taking the first update it does isn't very accurate?

Answer: YES. You have to check the "age" of location (like @Foster Bass) and desired accuracy before using it.

Sometime, you will see the "age" of first location is hundred of seconds passed from now.

Some more issues you may know here. Hope it help.

Upvotes: 2

Foster Bass
Foster Bass

Reputation: 908

This delegate method is typically called first with cached data, then again with updated location data.

You can check to see how old the location data is before using it. For example:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
   NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
   // If this location was made more than 3 minutes ago, ignore it.
   if (t < -180) {
      // This is cached data, you don't want it, keep looking
      return;
   }

  [self foundLocation:newLocation];
}

Also, if you've requested a high level of accuracy from the CLLocation Manager, the didUpdateToLocation delegate will get called multiple times as the accuracy is refined. If you really only want the first one (which probably isn't the case), set a boolean to keep track of the fact that you've already received a location so you can ignore subsequent calls.

Upvotes: 3

Related Questions