控制位置管理器更新间隔

发布于 2024-11-25 21:33:54 字数 107 浏览 0 评论 0原文

有没有办法控制位置管理器的更新间隔?为了提高准确性,我使用了最近的 10 米设置,但我的应用程序似乎每秒都会更新一次,我认为这种情况太频繁了。我想要良好的准确性,但不想要频率。

谢谢。

Is there a way to control the update interval of the location manager? I used the nearest 10 meter setting for accuracy but it appears that my application gets one update every second, which I think is too often. I want the good accuracy but not the frequency.

Thanks.

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

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

发布评论

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

评论(1

海夕 2024-12-02 21:33:54

我用计时器做到了:

在标头中添加:

ServerInterface *serverInterface;

在 .m 文件中:

- (void)viewDidLoad
{
...
[frequentLocationUpdateTimer invalidate];
frequentLocationUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:
               @selector(reEnableLocationTick:) userInfo:nil repeats:YES];
...
}
- (void)reEnableLocationTick:(NSTimer *)theTimer
{
    [locationGetter startUpdates]; //Maybe you have to change this line according to your code
} 

以及在您的位置管理器中...:

- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    ...

        if (newLocation.horizontalAccuracy < 100)
    {
        [locationManager stopUpdatingLocation];  
    } 
    ...

    // let our delegate know we're done
    [delegate newPhysicalLocation:newLocation];
}

代码在达到 100m 的最小精度后停止更新位置。然后,每隔 30 秒,计时器就会再次重新启用位置管理器。

I did it with a timer:

in the header add:

ServerInterface *serverInterface;

in the .m file:

- (void)viewDidLoad
{
...
[frequentLocationUpdateTimer invalidate];
frequentLocationUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:
               @selector(reEnableLocationTick:) userInfo:nil repeats:YES];
...
}
- (void)reEnableLocationTick:(NSTimer *)theTimer
{
    [locationGetter startUpdates]; //Maybe you have to change this line according to your code
} 

and in your locationmanager...:

- (void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    ...

        if (newLocation.horizontalAccuracy < 100)
    {
        [locationManager stopUpdatingLocation];  
    } 
    ...

    // let our delegate know we're done
    [delegate newPhysicalLocation:newLocation];
}

The code simply stops updating the location after reaching a minimum accuracy of 100m. Then, every 30sec a timer reenables the locationmanager again.

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