停止核心位置更新,然后使用计时器重新启动它们

发布于 2024-08-30 04:29:27 字数 257 浏览 0 评论 0原文

我想知道是否有人可以向我指出(或粘贴)一些代码来处理关闭核心位置更新以节省电量。

据我了解,一旦获得所需的精度读数,您就应该停止核心位置更新。如果您在一定时间后没有获得良好的准确度读数,您还应该停止更新(大概使用计时器)。每次停止更新时,您应该触发一个计时器(大约 60 秒)来重新启动 Core Location 并获取新的读数。

苹果有没有代码可以完成这一切? LocateMe、TaggedLocations 和 Locations 示例代码似乎没有做到这一点。

I was wondering if anyone could point me to (or paste in) some code to deal with turning off Core Location updates to save power.

As far as I understand it, you should stop Core Location updates as soon as you get a reading of desired accuracy. If you don't get a good accuracy reading after a certain time, you should also stop updates (presumably using a timer). Every time you stop updates, you should fire a timer (around 60 seconds) to restart Core Location and get a new reading.

Is there Apple code which does all this? The LocateMe, TaggedLocations and Locations sample code don't seem to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-09-06 04:29:27

LocateMe 示例包含您需要的代码。您只需要创建第二个选择器即可触发。
LocateMe 在其设置方法中调用以下内容...

    [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:[[setupInfo objectForKey:kSetupInfoKeyTimeout] doubleValue]];

它表示在一定时间 (kSetupInfoKeyTimeout) 后,请调用 stopUpdatingLocation 方法,参数为 NSString = "Timed Out"。
stopUpdatingLocation 方法内,调用 [locationManager stopUpdatingLocation] 来告诉 CoreLocation 停止。

因此,您需要做的就是

[self performSelector:@selector(timeToRestartCoreLocation) afterDelay: 60];

stopUpdatingLocation 方法中添加另一个像这样的选择器...,该方法将在 60 秒后调用 timeToRestartCoreLocation 方法。然后在 timeToRestartCoreLocation 方法中,调用 [locationManager startUpdatingLocation] 再次启动 CoreLocation。

The LocateMe example has the code you need. You just need to create a second Selector to Fire.
LocateMe calls the following in it's setup method...

    [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:[[setupInfo objectForKey:kSetupInfoKeyTimeout] doubleValue]];

It says that after a certain amount of time (kSetupInfoKeyTimeout), please call the stopUpdatingLocation method the the argument of NSString = "Timed Out".
Inside the stopUpdatingLocation method, the [locationManager stopUpdatingLocation] is called to tell CoreLocation to stop.

So, all you need to do is add another Selector like this...

[self performSelector:@selector(timeToRestartCoreLocation) afterDelay: 60];

inside the stopUpdatingLocation method, which will call the timeToRestartCoreLocation method after 60 seconds. Then inside your timeToRestartCoreLocation method, call [locationManager startUpdatingLocation] to kick off CoreLocation again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文