简体   繁体   中英

didUpdateToLocation not called second time

In our app, I'm using CoreLocation to get user's location. I thought the call to startUpdatingLocation would invoke locationManager:didUpdateLocations every time. But once it's getting called for first time, they never get called anymore.

In my AppDelegate.m:

- (void) startStandardUpdates {
 if (_locationManager == nil) {
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager setDelegate:self];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = 10;
    [_locationManager startUpdatingLocation];

 } else {
    NSLog(@"[LOCATION]: Location Manager already exists.");
    [_locationManager startUpdatingLocation];
 }
}

Then call it by NSTimer:

- (void) startTask {
  NSLog(@"Start task called.");
  [NSTimer scheduledTimerWithTimeInterval:15
                                   target:self
                                 selector:@selector(actualTask)
                                 userInfo:nil
                                  repeats:YES];
}

- (void) actualTask {
  NSLog(@"[TASK]: Starting location service...");
  [self startStandardUpdates];
}  

The above code should call startUpdatingLocation every 15 seconds but the didUpdateToLocation method not getting called for the second time (the first one successfully called...).

Is there a way to call (or force) didUpdateToLocation 'programmatically'? Or we have to wait until didUpdateToLocation gets the location? I have searched around the way to get user's location every x minutes/seconds but no one seems to successfully achieved that. What I would like to do is just the equivalent of how Android do it with Service class (iOS doesn't have this).

Any help would be appreciated.
Thank you.

_locationManager.distanceFilter=10; your distance filter is set to 10m you will not be notified. Once the position has moved enough to exceed your distance filter setting then you will get a didUpdateToLocation callback...

Hmm. Once you startUpdatingLocation it's going. Pretty much non-stop, based on the accuracy you selected, so there isn't a need to keep starting it. It will wait for significant location changes that match your accuracy... it handles all this efficiency and power tradeoff stuff for you. Did you try using the simulator and selecting the driving route option? It might help you get more of a sense of it in motion (only start it once).

If you simply need it to give you that ping every 15 seconds then maybe start updating... and then simply read the current location/heading every 15 seconds in your timer?

- (void) actualTask {
    NSLog("Currently at: %@", MyLocationManager.location.coordinate);
}

I think if you are in building or complex then didUpdateLocations will not called. if you can real time test like take build on device on just move on to open space so GPS can get the data and you will receive the data. if you walk then it also not get frequently didUpdateLocations . you have to test on car/bus or in bike.

在您的代码中,将distancefilter=10更改为kCLDistanceFilterNone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM