I am using core locations to get the location of the iphone. I also implemented the delegate protocols to my viewcontroller files, and created a CLLocationmanager object..
When I press a button (IBAction), I want the location get updated. The delegate gets called correctly. I can see that because a NSLog messages appears with the lat/long coordinates.
But when I change the location in the IOS simulator and try to access the location again with the button, the location output I get remains the same. I have to push the button again to get the correct location. Obviously I want the location to be right the first time I push the button
This is my button that triggers the delegate:
- (IBAction)getLocation:(id)sender {
[locationManager startUpdatingLocation];
}
I have this piece of code in my viewDidLoad
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
This is my delegate method:
#pragma mark - CLLocationManagerDelegate
- (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 didUpdateLocations:(NSArray *)locations{
NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
CLLocation *currentLocation = [locations lastObject];
if (currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
[locationManager stopUpdatingLocation];
}
I'm running my code in Iphone 6.1 simulator..