Why Self.Locationmanager Stopupdatinglocation Doesn't Stop Location Update

why self.locationManager stopUpdatingLocation doesn't stop location update

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

You probably want to replace startMonitoringSignificantLocationChanges with startUpdatingLocation for the sake of more regular updates, unless you have a specific reason for monitoring only for significant location changes.

Check out the CLLocation documentation for further detail.

How to stop updating location once I get current location?

After getting your location, use this method:

[self.locationManager stopUpdatingLocation];
self.locationManager = nil;

When should I stop updating location manager?

Your Distance filter of the location manager is set to be kCLDistanceFilterNone. This causes the didUpdateLocations method to be called infinite time.

locationManager.distanceFilter = kCLDistanceFilterNone;

Change this line as

locationManager.distanceFilter = 10;

and try again. Change the value as needed.

So now the didUpdateLocation will not be called infinite times. :)

Hopefully this helps.

CLLocationManager stopUpdatingLocation doesn't stop

didUpdateLocations: can be called frequently and at any time because of its async nature and optimisations :

Because it can take several seconds to return an initial location, the
location manager typically delivers the previously cached location
data immediately and then delivers more up-to-date location data as it
becomes available. Therefore it is always a good idea to check the
timestamp of any location object before taking any actions. If both
location services are enabled simultaneously, they deliver events
using the same set of delegate methods.

Actually, if you uncomment locationManager = nil; it should help, but if you want to use your checkingForLocation property, you should make the assignment synchronised. @synchronized(self) is the easiest way in this case.

Location still monitoring after locationManager = nil and stopUpdatingLocation called

One possibility: The blue dot moving on your map is because you set the MKMapView's showsUserLocation to YES. It will track until you set it to NO.

Another possibility: This line is wrong:

locationManager = nil;

That does not stop monitoring, but it does cause you to be unable to refer to the location manager, so now you can't stop monitoring! Cut that line.

CLLocationManager not updating location

You are stopping the location manager with

[self.locationManager stopUpdatingLocation];

in

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc

. This delegate method gets called EVERY time the user moves and can give you old (cached) data at the beginning, so by stopping it right away when you get your first location fix you probably get a cached location each time. The fix is simple, just don't stop the location manager there but rather when your viewController disappears or at a similar useful place.

CLLocationManager don't stop

You do it the right way. When entering background, it's ok if some delegate methods are called for some seconds. That should stop.

Where do you stop the location updates ? Are you sure it is triggered ? If yes, are your delegate method called even if the visual indicators tell something else ?

Are you sure you don't trigger a method that reactivate the location update after you have stopped it (because for example you can receive some updates even after stopped).

How to stop location manager?

Is it possible your activity isn't being destroyed? i.e.: you hit the home button. Move your gps start/stop to onStart and onPause.

Updating speed only after button tapped in Swift

I'm not sure this is the best solution, but you might use an extra variable to make sure that when you are getting the location on app start, the speed and distance calculations should not be done. Something like this:

let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()

isToPerformCalculations = false
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

@IBAction func startStopButtonDidTouch(_ sender: UIButton) {

if isStarted { //When tapped STOP
// locationManager.stopUpdatingLocation()
startStopButton.setTitle("START", for: .normal)
} else { //When tapped START
isToPerformCalculations = true
startStopButton.setTitle("STOP", for: .normal)
}
}

func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
locationManager.stopUpdatingLocation()
if isToPerformCalculations {
if (startLocation == nil) {
startLocation = location;
}
let endLocation = location;


speedLabel.text = "\(Int(location.speed * 3.6))"
let distance = startLocation.distance(from: endLocation) / 1000 //m->km
distanceLabel.text = "\(String(format: "%.1f", distance)) km"
}
}

I also changed where you stopUpdatingLocation, see if this works for you.



Related Topics



Leave a reply



Submit