I'm trying to create an app that will gather and display my longitude and latitude values in the labels I have set in the storyboard. If i simply assign a simple text to my labels in my IBAction instance, it runs fine and the text is displayed. However, when I use the code in my IBAction instance below, nothing is displayed in my labels when I press my button.
@interface ViewController ()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
@synthesize longitude, latitude;
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
@end