Yuvraj Kale
Yuvraj Kale

Reputation: 49

Why am I sometimes getting inaccurate latitude and longitude values from CLLocationManager?

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;


if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
    [self.locationManager requestAlwaysAuthorization];

}
[locationManager startUpdatingLocation];

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    if(newLocation.horizontalAccuracy<0)
    {
        [locationManager startUpdatingLocation];
    }
    else
    {
        NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];

        if(abs(interval)<15)
        {
//Here i am doing my code but i get location which is far from my current location last time maximum distance i got from the current location was near about 3000 meter

            [locationManager stopUpdatingLocation];
        }
    }
}

i use this code but not getting the location accurate some time it gives more than 1 km away from current location i want accurate location

Upvotes: 2

Views: 664

Answers (1)

Duncan C
Duncan C

Reputation: 131426

When you first ask for location updates you may get a "stale" location from the last time the GPS was active. (I've seen stale location readings that are several kilometers off.) The first fiew locations also tend to have poor accuracy.

You should check the date stamp on the locations you get and reject any that are older than 1 second old, and also reject those who's accuracy reading is larger than your desired accuracy.

EDIT:

Your didUpdateToLocation method does not make sense.

When you call startUpdatingLocation, you will get location updates as the location changes, until you call stopUpdatingLocation

There is no reason to call startUpdatingLocation inside your didUpdateToLocation method, because the location is already being updated. In fact it might mess things up. Don't do that.

In pseudocode, what you want to do is something like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  if the time of the update is > 5 seconds old, return.

  if horizontalAccuracy < 0 || horizontalAccuracy > 500, return. 

  stop updating location.
  
  do whatever you want to do with the location.
}

When you return without doing anything, you'll get more location updates as the GPS settles down.

I use 500 as the maximum acceptable accuracy reading as an example. That would be .5 KM, which is a lot of error. A smaller number like 100 or 50 would give better results, but take longer.

Upvotes: 3

Related Questions