Location.HorizontalAccuracy is too bad when you get out of the background

I am writing an application that controls the location of a user. I have a CLLocationManager object that uses startMonitoringSignificantLocationChanges, so I can get location updates from the background when the application is not running. I have installed my application didFinishLaunchingWithOptions, so if I get the location key, I start my manager to get the user's location. Everything works fine, but the problem is that every time I get a location from the background, the horizontal accuracy of this location is very poor. In most cases, it is 1414 m.

Does anyone know why horizontal accuracy is so bad when the location is due to the background? Is there anything I can do to get places with better accuracy in the background?

When the application works, all the locations that I get are very accurate, this only happens when the location comes from the background. Does this have anything to do with the number of cells that I have in my city? I thought maybe the device doesn’t use GPS wifi nodes to get places in the background.

In any case, any help here is appreciated. Please share your thoughts.

Thanks!

+8
ios objective-c iphone xcode cllocationmanager
source share
2 answers

The accuracy of the locations returned by the CLLocationManager is determined by the desiredAccuracy , which by default is kCLLocationAccuracyBest , and the device accuracy available. For example, you can get less accurate locations if the device’s battery is running low, or you can get more accurate locations if they are still cached from another application.

However, obtaining incredibly accurate coordinates depletes a significant portion of the energy from the battery and dumps the device. Applications in the background are probably limited by a much lower accuracy resolution to improve battery performance.

Accurate locations require a lot of power to use GPS radio, while less accurate locations can rely on nearby Wi-Fi access points and cell phone towers within the range of the phone.

When your application resumes from the background, the system will try to increase the accuracy of the results. This is a complex concept, but take a look at the Maps app on your phone. At first, the circle representing your location is very large; as the system gets a more accurate picture of your location, the circle gets smaller. This visualization is a phone with more power to get a more accurate location.

You will see a similar phenomenon with CLLocationManager when your application resumes from the background: you will get an inaccurate location and receive subsequent, more accurate updates.

This is a trade-off between the convenience and battery life that Apple should have when developing its APIs. The first update for the user's location is likely to be less accurate, unless they just use the Maps application and the location is cached.

The best advice I can give you is to listen to subsequent updates from your location manager and update your interface accordingly. Good luck

+11
source share

As the name indicates: The startMonitoringSignificantLocationChanges notification is only available there so that you know that the user's location is significantly different from the last one set for verification. It is your job when you receive this notification to update your location in accordance with the desired accuracy you want. Notification will not do this for you. This will only let you know that the location has changed so that you can handle the situation. If you don’t know how to get the best accuracy, you can check the Apple code sample for LocateMe.Here snippet that keeps the accuracy (bestEffortAtLocation) and then checks the accuracy every time the delegate is called until the best result or timeout appears.

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // store all of the measurements, just so we can see what kind of data we might receive [locationMeasurements addObject:newLocation]; // test the age of the location measurement to determine if the measurement is cached // in most cases you will not want to rely on cached measurements NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; if (locationAge > 5.0) return; // test that the horizontal accuracy does not indicate an invalid measurement if (newLocation.horizontalAccuracy < 0) return; // test the measurement to see if it is more accurate than the previous measurement if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) { // store the location as the "best effort" self.bestEffortAtLocation = newLocation; // test the measurement to see if it meets the desired accuracy // // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout. // if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) { // we have a measurement that meets our requirements, so we can stop updating the location // // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible. // [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")]; // we can also cancel our previous performSelector:withObject:afterDelay: - it no longer necessary [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil]; } } // update the display with the new location data } 

Apple owns the loan because it’s a snippet directly from its LocateMe sample code:

http://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801-Intro-DontLinkElementID_2

So, when you receive a notification and you need to get the best result, you will need to update the accuracy and see if it gives the best result.

+9
source share

All Articles