How to stop updating location once I get current location?
12,386
Solution 1
After getting your location, use this method:
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
Solution 2
BOOL first_time = YES; // public
Every time you start updating location set first_time to YES:
first_time = YES;
[self.locationManager startUpdatingLocation];
in your didUpdateUserLocation method:
if (userLocation == nil) {
NSLog(@"User location is nil. maybe wating for permission");
} else if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
NSLog(@"User location is not valid 2d coordinates. maybe called in background");
} else {
NSLog(@"Did update user location: %f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
// here is the first time you receive user location
if (first_time)
{
first_time = NO;
[self.locationManager stopUpdatingLocation];
}
}
Solution 3
Call stopUpdatingLocation
as soon as your didUpdateLocations
method is called.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[manager stopUpdatingLocation];
//store your location
self.location = [locations lastObject];
}
Solution 4
call below method to save and stop location after you get it once
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
self.location = [locations lastObject]
self.locationManager.delegate = nil;
[self.locationManager stopUpdatingLocation];
}
Author by
durazno
Updated on June 14, 2022Comments
-
durazno almost 2 years
I'm using Parse and with geoPointForCurrentLocationInBackground I can stop updating once a location is received without having to manually stop it.
How do I stop updating location immediately right after I receive location using CLLocationManager?
Edit
I know [self.locationManager stopUpdatingLocation]; stops it. What I'm really asking is, how do I know I've received location for the first time then stop it immediately?
-
Nat over 8 yearsAlso, as you don't need locationManager any more, you may release it. Firstly set the delegate to nil.
self.locationManager.delegate = nil; self.locationManager = nil
. -
Vishnuvardhan over 8 years-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ if(locations.count>0){ [self.locationManager stopUpdatingLocation]; self.locationManager = nil; } } try like this
-
Nat over 8 yearsThat's not enough. Setting delegate to nil has it's purpose. Please read the references like: developer.apple.com/library/ios/documentation/UIKit/Reference/… or stackoverflow.com/a/15016656/849616. These are about different components (what doesn't matter), but that's a good practice and may fix a few rare crashes.
-
Nat over 8 yearsHow do you know he's using
startMonitoringSignificantLocationChanges
? Besides, I think you've swapped the method. It should be: the opposite ofstart...
is notstopUpdating...
butstopMonitoring...
. -
durazno over 8 yearsIs setting its delegate nil necessary? Does that mean next time I call didUpdateLocations I have to set delegate again beforehand? Why would I set it nil if I'm gonna have to re-set it again in the future?
-
durazno over 8 yearsBut does't stopUpdatingLocation shuts it down anyway..?
-
durazno over 8 yearsIs setting its delegate nil necessary? Does that mean next time I call didUpdateLocations I have to set delegate again beforehand? Why would I set it nil if I'm gonna have to re-set it again in the future?
-
Mahesh over 8 yearsThe setting to nil has really nothing to do with retain release cycle, it is simply to avoid locationManager sending delegate call to your controller. If you want again used locationManager then you have to again set delegate.