Search code examples
labelcllocation

Accuracy of a location in a label


I am still a beginner developer in xcode and the entire objective-C.

I am making an app that you can see your coordinates on a screen ( its still in the beginning phase)

But the user cant see how accurate the coordinates are... I can see in the debug area, but the user that uses the app cant see it.

How do I add that in my code?

My code is currently :

#import "CurrentLocationViewController.h"

@interface CurrentLocationViewController ()

@end

@implementation CurrentLocationViewController
{
CLLocationManager *_locationManager;
CLLocation *_location;

BOOL _updatingLocation;

NSError *_lastLocationError;


}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self updateLabels];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)getLocation:(id)sender
{
[self startLocationManager];
[self updateLabels];

}


- (id) initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
    _locationManager = [[CLLocationManager alloc] init];

}
  return self;
}

pragma mark - CLLocationManagerDelegate

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"did fail with error %@",error);

if (error.code == kCLErrorLocationUnknown) {
    return;
}

[self stopLocationManager];
_lastLocationError = error;

[self updateLabels];

}

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSLog(@"didUpdateLocations %@", newLocation);

_lastLocationError = nil;
_location = newLocation;
[self updateLabels];
}


- (void) updateLabels

{
if (_location !=nil) {
    self.latitudeLabel.text = [NSString     stringWithFormat:@"%.8f",_location.coordinate.latitude];
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",_location.coordinate.longitude];
    self.tagButton.hidden = NO;
    self.messageLabel.text = @"Here are your coordinates";
}
else {
    self.latitudeLabel.text= @"";
    self.longitudeLabel.text= @"";
    self.adressLabel.text= @"";
    self.tagButton.hidden = YES;

    NSString *statusMessage;
    if (_lastLocationError != nil) {
        if ([_lastLocationError.domain isEqualToString:kCLErrorDomain] && _lastLocationError.code == kCLErrorDenied) {
            statusMessage = @"Location Services Disabled";
        } else {
            statusMessage = @"Error getting location";
        }
    } else if (![CLLocationManager locationServicesEnabled]) {
        statusMessage = @"Location Services Disabled";
    } else if (_updatingLocation) {
        statusMessage = @"Searching...";
    } else {
        statusMessage = @"Press the Button to start";
    }

    self.messageLabel.text = statusMessage;

}

}

- (void) startLocationManager
{
if ([CLLocationManager locationServicesEnabled]) {
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [_locationManager startUpdatingLocation];
    _updatingLocation = YES;
}
}


- (void) stopLocationManager
{
if (_updatingLocation) {
    [_locationManager stopUpdatingLocation];
    _locationManager.delegate = nil;
    _updatingLocation = NO;
}
}

@end

@end


Solution

  • horizontalAccuracy is a property of CLLocation. It reports the current horizontal accuracy in meters. The meaning of this is that your true position is up to horizontalAccuracy meters from the reported position in any direction.
    Referring to your updateLabels code, you can access _location.horizontalAccuracy and display it along with the other information.
    There is a similar property verticalAccuracy.