Home > IOS, Location Services > Initial GPS fix always incorrect : Corelocation, CLLocationManager

Initial GPS fix always incorrect : Corelocation, CLLocationManager

The Problem:

You use CLLocationManager.StartUpdatingLocation( ) to get an initial fix on the user’s location in your code. But you notice that the location is always wrong and way-off.

Analysis:

Take a look at Apple’s own documentation of CLLocationManager here : (link to documentation)

Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. Therefore it is always a good idea to check the timestamp of any location object before taking any actions. If both location services are enabled simultaneously, they deliver events using the same set of delegate methods.

Basically, IOS returns you previously cached information. This could also mean that if you used Corelocation in San Francisco today and then used the app in Dallas next, it will still report your location in San Francisco.

Solution:

In my code below, I have used two ways of making sure I have the correct location. One is to make sure horizontalAccuracy is < 100 meters and the next is the location timestamp is < 15 seconds ago. If it falls under any of these two conditions, the location is probably wrong and can be discarded. In this case I just wait for IOS to give me a fresher more accurate location fix.

Point to be noted here is that because of this, be prepared to wait for upto 10 seconds depending on how accurate the location info you want to get. More accurate = more delay especially after a cold start. Chances are, if you are not writing a GPS turn-by-turn navigation app, you don’t need pin-point location info. So, play around with it and relax the accuracy as much as possible depending on what fits your app’s scenario.

– (void) locationManager:(CLLocationManager*) manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
{
@try
{
if(newLocation.horizontalAccuracy > 100)
{
NSLog(@”Ignoring GPS location more than 100 meters inaccurate :%f”, newLocation.horizontalAccuracy);
return;
}

NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
NSLog(@”Ignoring GPS location more than 15 seconds old(cached) :%d”, abs(howRecent));
return;
}

[myLM stopUpdatingLocation];
}
@catch (NSException* ex)
{
[self debugMessage:ex.reason withTitle:@”Uncaught Error in didUpdateToLocation()”];
}
}

Advertisements
  1. Noureddine
    July 3, 2011 at 5:45 am

    awesome, i was looking for,,, not only the FIX that its not correct, it is also the heading..i think applying your rule up there will work for both, thanx for sharing..

  2. July 8, 2011 at 10:47 am

    Your code snippet seems to have logical error

    if (abs(howRecent) < 15.0)

    Probably, this check should be like this

    if (abs(howRecent) > 15.0)

  3. January 10, 2012 at 1:08 pm

    We were also stuck with similar wrong location problem since a long time. We were just setting DesiredAccuracy to BestAccuracy and thought it should be enough. For reasons I am still not sure about it looks like DesiredAccuracy is not what you get, so we need to compare HorizontalAccuracy as well. We are now going to give it a try and hopefully it should solve our problem as well. Thanks for sharing this tip. You have been of a great help.

    • Dick Wong
      May 22, 2012 at 1:24 pm

      I faced the same problem now. Any solution finally ?

  4. arjun
    May 12, 2012 at 5:52 am

    thanks mate for your post
    i was having the same problem
    hope it solves

  1. May 12, 2012 at 7:47 pm
  2. March 20, 2013 at 3:13 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: