0

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
Ajumal
  • 1,048
  • 11
  • 33
  • Are you running on simulator, then you need to set your location from Menu - >Debug - > Location – Ajumal Apr 25 '15 at 04:45
  • Is this on iOS 8? If so, you must set certain info.plist keys and call requestXXX first. See http://stackoverflow.com/questions/24062509/location-services-not-working-in-ios-8. Also, didUpdateToLocation is deprecated, use didUpdateLocations instead. –  Apr 25 '15 at 11:18

0 Answers0