iPhone 区域监控 - 区域未添加?

发布于 2024-10-08 21:46:09 字数 1221 浏览 3 评论 0原文

我正在尝试设置区域监控。看起来很简单;但是当我检查我监视的区域数量时,计数始终为 0。

代码:

if ([CLLocationManager regionMonitoringAvailable] &&
    [CLLocationManager regionMonitoringEnabled] ) {

    CLLocationCoordinate2D coordinate;
    double radius;

    for(Item *item in ad.proxiArray) {

        radius = [item.distance floatValue];

        coordinate= CLLocationCoordinate2DMake([item.latitude doubleValue],
                                                [item.longitude doubleValue]);
        CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:radius identifier:item.place];

        NSLog(@"Adding: %@", region);

        [self.locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyNearestTenMeters];

        [region release];
    }   
    NSLog(@"region count %i",[[self.locationManager monitoredRegions] count]);
    for (CLRegion *re in [self.locationManager monitoredRegions]) {
        NSLog(@"Monitoring: %@", re);
    }
} else {
    NSLog(@"Region monitoring unavailable");
}

运行时,NSLog 将显示我实际上正在添加所有项目(大约 10 个)。但在循环结束时,如上所述:

2010-12-21 12:14:38.172 xxxxxx[8112:307] 区域计数 0

怎么了???

I'm trying to set up region monitoring. Looks pretty straightforward; but when I check the number of regions I monitor, the count is always 0.

Code:

if ([CLLocationManager regionMonitoringAvailable] &&
    [CLLocationManager regionMonitoringEnabled] ) {

    CLLocationCoordinate2D coordinate;
    double radius;

    for(Item *item in ad.proxiArray) {

        radius = [item.distance floatValue];

        coordinate= CLLocationCoordinate2DMake([item.latitude doubleValue],
                                                [item.longitude doubleValue]);
        CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:radius identifier:item.place];

        NSLog(@"Adding: %@", region);

        [self.locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyNearestTenMeters];

        [region release];
    }   
    NSLog(@"region count %i",[[self.locationManager monitoredRegions] count]);
    for (CLRegion *re in [self.locationManager monitoredRegions]) {
        NSLog(@"Monitoring: %@", re);
    }
} else {
    NSLog(@"Region monitoring unavailable");
}

When run, NSLog will show that I am actually adding all items (about 10). But at the end of the loop, as stated above:

2010-12-21 12:14:38.172 xxxxxx[8112:307] region count 0

What's wrong????

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

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

发布评论

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

评论(1

冷血 2024-10-15 21:46:09

我的印象是

  • CLLocationManager 本身不是线程安全的。区域监控和位置监控会干扰,有时可能会导致阻塞(根据我的观察,一定有超时,因为程序在 30 秒多一点后继续工作)
  • 区域将异步添加,从而检查数量添加完成后直接添加区域可能会导致错误的结果。
  • 实际添加监控区域时,精度很差(几公里的分辨率)。

由于这些问题以及区域监控不太准确,我放弃了使用该功能的想法。

My impression is that

  • CLLocationManager in itself is not thread-safe. Region monitoring and location monitoring will interfere and may occasionally lead to blocks (from my observation, there must be kind of a timeout, as the program continues to work after a bit more than 30 seconds)
  • Regions will be added asynchronously, thus checking the number of regions directly when having finished adding them may lead to a wrong result.
  • The accuracy is very bad (resolution of several km) when regions are actually added to be monitored

Because of these problems and the fact that region monitoring is not very accurate, I gave up the idea of using that feature.

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