3

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?

durazno
  • 559
  • 2
  • 7
  • 25

8 Answers8

12

After getting your location, use this method:

[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
emotality
  • 12,795
  • 4
  • 39
  • 60
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • Also, 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`. – Nat Aug 10 '15 at 09:55
  • -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ if(locations.count>0){ [self.locationManager stopUpdatingLocation]; self.locationManager = nil; } } try like this – Vishnuvardhan Aug 10 '15 at 10:00
  • That's not enough. Setting delegate to nil has it's purpose. Please read the references like: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/ or http://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 Aug 10 '15 at 10:01
  • Is 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 Aug 10 '15 at 10:16
2

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];
}
Josh Heald
  • 3,907
  • 28
  • 37
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];
    }
}
hasan
  • 23,815
  • 10
  • 63
  • 101
1

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];
}
Mahesh
  • 956
  • 10
  • 17
  • Is 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 Aug 10 '15 at 12:46
  • The 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. – Mahesh Aug 11 '15 at 04:34
0

The opposite of stopMonitoringSignificantLocationChanges is not stopUpdatingLocation, it is startMonitoringSignificantLocationChanges.

Check out the CLLocation documentation for further detail.

Nat
  • 12,032
  • 9
  • 56
  • 103
Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40
  • 1
    How do you know he's using `startMonitoringSignificantLocationChanges`? Besides, I think you've swapped the method. It should be: the opposite of `start...` is not `stopUpdating...` but `stopMonitoring...`. – Nat Aug 10 '15 at 10:06
0

Finally found the answer in another question...reprinting it here because it took me a while to stumble on it.

I had to call this:

[_locationManager stopMonitoringSignificantLocationChanges];

Even though I never called startMonitoringSignificantLocationChanges in the first place, seems I had to "un-call" it...strange that it works this way, but as soon as I added that line, location services shut down promptly. Hope this helps someone else.

Rohit suvagiya
  • 1,005
  • 2
  • 12
  • 40
0

Once current location update stop location manager using stopUpdatingLocation.

region.center = self.mapView.userLocation.coordinate;
if (region.center.longitude != 0 && region.center.latitude != 0) {
    [self.locationManager stopUpdatingLocation];
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
johnny
  • 555
  • 4
  • 16
0
 self.yourLocationManager.stopUpdatingLocation()
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52