简体   繁体   English

didUpdateToLocation没有第二次调用

[英]didUpdateToLocation not called second time

In our app, I'm using CoreLocation to get user's location. 在我们的应用程序中,我使用CoreLocation来获取用户的位置。 I thought the call to startUpdatingLocation would invoke locationManager:didUpdateLocations every time. 我认为对startUpdatingLocation的调用每次都会调用locationManager:didUpdateLocations。 But once it's getting called for first time, they never get called anymore. 但是一旦它第一次被召唤,他们就再也不会被召唤了。

In my AppDelegate.m: 在我的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: 然后由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...). 上面的代码应该每15秒调用一次startUpdatingLocation,但是第二次没有调用didUpdateToLocation方法(第一次成功调用了...)。

Is there a way to call (or force) didUpdateToLocation 'programmatically'? 有没有办法以编程方式调用(或强制)didUpdateToLocation? Or we have to wait until didUpdateToLocation gets the location? 或者我们必须等到didUpdateToLocation获取位置? I have searched around the way to get user's location every x minutes/seconds but no one seems to successfully achieved that. 我已经搜索了每隔x分钟/秒获取用户位置的方式,但似乎没有人成功实现这一点。 What I would like to do is just the equivalent of how Android do it with Service class (iOS doesn't have this). 我想做的只是Android与Service类的相关(iOS没有这个)。

Any help would be appreciated. 任何帮助,将不胜感激。
Thank you. 谢谢。

_locationManager.distanceFilter=10; _locationManager.distanceFilter = 10; your distance filter is set to 10m you will not be notified. 您的距离过滤器设置为10米,您将不会收到通知。 Once the position has moved enough to exceed your distance filter setting then you will get a didUpdateToLocation callback... 一旦位置移动到足以超过距离过滤器设置,您将获得didUpdateToLocation回调...

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? 如果你只是需要它每15秒给你一次ping,那么可能会开始更新......然后只需在你的计时器中每15秒读一次当前的位置/标题?

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

I think if you are in building or complex then didUpdateLocations will not called. 我想如果你在建造或复杂,那么didUpdateLocations将不会被调用。 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. 如果您可以进行实时测试,例如在设备上进行开放空间,那么GPS可以获取数据,您将收到数据。 if you walk then it also not get frequently didUpdateLocations . 如果你走路,那么它也不会经常使用didUpdateLocations you have to test on car/bus or in bike. 你必须在汽车/公共汽车或自行车上进行测试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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