Why do CLLocation coordinates lose precision when displayed?
I'm working on some experiments to try and increase the GPS accuracy on an iPhone by performing weighted averages on data collected over time. I've noticed a difference in the display of coordinates in the console, on the iPhone simulator, and on the iPhone itself.
I'm running basic CoreLocation code to set up my Location Manager.
self.locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
In my locationManager: didUpdateToLocation: fromLocation: method I write the coordinates to the console in several formats using NSLog before displaying them in my IBOutlets.
NSLog(@"%@", newLocation);
NSLog(@"%f", newLocation.coordinate.latitude);
NSLog(@"%e", newLocation.coordinate.latitude);
NSLog(@"%E", newLocation.coordinate.latitude);
self.latitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
self.longitude.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
self.altitude.text = [NSString开发者_如何学C stringWithFormat:@"%f", newLocation.altitude];
self.horizontalAccuracy.text = [NSString stringWithFormat:@"%f", newLocation.horizontalAccuracy];
self.verticalAccuracy.text = [NSString stringWithFormat:@"%f", newLocation.verticalAccuracy];
The values that are written to the console are as follows.
NSLog(@"%@", ...); | <+42.40334972, -71.27483790> +/- 240.00m (speed -1.00 mps / course -1.00) @ 2010-10-19 12:15:00 GMT
NSLog(@"%f", ...); | 42.403350
NSLog(@"%e", ...); | 4.240335e+01
NSLog(@"%E", ...); | 4.240335E+01
The latitude and longitude displayed onscreen are as follows.
Latitude | 42.403350
Longitude | -7.274838
Since the latitude and longitude are CLLocationDegrees, i.e. doubles, and I'm using the %f to write the coordinates to the console and display them onscreen, I didn't think the "raw" coordinates would be rounded up from 8 to 6 decimal places. (I also don't know if the GPS receiver on the iPhone collects coordinates at the same level of precision as the MacBook Pro.)
Obviously I can store the CLLocation objects in an array and use the more precise coordinates in my weighted average calculations but I also wanted to display the increased precision onscreen. Any workarounds or thoughts?
Thanks in advance.
6 digits is just a default value for floating point output using NSLog (and printf function) and the coordinate values stored in newLocation variable do not loose their precision, you can explicitly set the number of digits you need to print:
NSLog(@"%.8f", ...);// will print 8 decimal digits
精彩评论