I want my app to get current location when user taps a button. I initialize locationManager
object in init method.
First question: is this good if I'm going to need currentlocation every time I press button? Or should I initialize it even in viewDidLoad
?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
// Best accuracy as posibble
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
return self;
}
In my delegate method I stopUpdatingLocation
as soon as I got my currentLocation
.
// Delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations
{
CLLocation *currentLocation = [locations lastObject];
[locationManager stopUpdatingLocation];
}
Here I startUpdatingLocation
with button:
- (IBAction)getCurrentLocation:(id)sender
{
[locationManager startUpdatingLocation];
}
Second question is: when I press button 1st time, I get right location, but when I change location in simulator and press again it shows the 1st one. Again, when I press the button, then it shows the right one. I have tried to initialize locationManager
every time button is pressed but it doesn't work that way neither.
I read on other post that its because of location cache or something like that. How do I remove that cache ? Because I'm going to store that location somewhere else in DB anyways so I don't need old one..