核心位置区域监控

Posted

技术标签:

【中文标题】核心位置区域监控【英文标题】:Core Location region monitoring 【发布时间】:2011-01-30 20:11:03 【问题描述】:

有谁知道使用这个的知识:

- (void) startMonitoringForRegion:(CLRegion *)region desiredAccuracy:(CLLocationAccuracy)accuracy

我正在尝试将其实施到我的项目中,但是:

- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

从未被调用?

有没有人有任何示例代码或知道为什么会这样?

我的代码如下。我在自己的 LocationManager 类中创建了这样的方法:

 - (void) locationManagerStartMonitoringRegion:(CLRegion *)region withAccuracy:(CLLocationAccuracy)accuracy 
    NSLog(@"Start Monitoring");
    [locationManager startMonitoringForRegion:region desiredAccuracy:accuracy];
    NSLog(@"Monitored Regions: %i", [[locationManager monitoredRegions] count]);

然后我这样称呼它:

CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(51.116261, -0.853758);     
CLRegion *grRegion = [[CLRegion alloc] initCircularRegionWithCenter:coordinates radius:150 identifier:[NSString stringWithFormat:@"grRegion%i", value]];

[locationManager locationManagerStartMonitoringRegion:grRegion withAccuracy:kCLLocationAccuracyBest];

我得到 NSLog 的:

2011-01-30 19:52:26.409 测试位置[10858:307] 开始监控

2011-01-30 19:52:27.103 测试位置[10858:307] 监控区域:

但永远不要从以下位置获取 NSLog:

 - (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region     
    NSLog(@"Entered Region");

 - (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error 
    NSLog(@"monitoringDidFailForRegion: %@",error);

谢谢

【问题讨论】:

你能发布你正在使用的代码吗?没有上下文就很难提供帮助。 代码现在添加到上面的原始帖子中。 您是否将 LocationManager 类设置为 CLLocationManager 的委托? @jodm 仍然面临这个问题。你有没有解决这个问题。你能触发 didEnterRegion。请提供一个工作示例 【参考方案1】:

您需要移动相当长的距离才能使区域监控功能正常工作。它当前的粒度似乎基于它何时从一个蜂窝塔转移到另一个蜂窝塔——在我的测试中,我必须移动一英里或更远才能让它记录我已经明确离开了我设置的一个小区域。

【讨论】:

今天早上在去办公室的路上我注册了大约 5 个点,其中 3 个被触发。情况确实如此。 didEnterRegion 仅在更改蜂窝塔或位置发生重大变化时调用。这对我想使用区域监控的上下文没有帮助:( @jodm,我知道这是一年多以前的事了,但情况好转了吗?我有同样的麻烦,我有一些不会触发或触发很晚的点。必须有更好的方法.. 我可以确认区域监控在一定程度上使用了重要位置,我反汇编了框架二进制文件并找到了与调用重要位置相关的内容。但即便如此,这还没有定论。【参考方案2】:

ios 5 中提高了准确性。

【讨论】:

您知道修复了什么吗?我在俄勒冈州尤金(约 150,000 人)仍然遇到麻烦,这让我相信它是基于手机信号塔和 WiFi 的。当然,如果是这样的话,那就太疯狂了,因为它会使区域监测只在人口密集的城市地区有用。 它确实可以在基于手机信号塔和 wifi 的 ios 5 和 6 中工作。它在人口稠密的城市地区效果最好。我唯一抱怨我的应用程序的地方是非常荒凉的地区(加拿大北部的一个岛屿)。【参考方案3】:

答案是:

- (void) startMonitoringForRegion:(CLRegion *)region desiredAccuracy:(CLLocationAccuracy)accuracy

在 ios 6.o 中已弃用。而是使用 `- (void) startMonitoringForRegion:(CLRegion *)region.

谢谢, 阿卜杜勒`

【讨论】:

developer.apple.com/library/ios/documentation/CoreLocation/…:【参考方案4】:

您可以监控多个区域并在 Xcode 中模拟位置(从调试器上方的面板)以检查它是否正常工作。我已经测试过,它运行得非常顺利。

【讨论】:

【参考方案5】:

我得看看你在哪里设置你的locationManager 实例。但正如@Mark Adams 试图逃避的那样,您需要将当前类设置为locationManager 的委托,以便它知道将消息发送回哪个类。很简单:

locationManager.delegate = self;

【讨论】:

以上是关于核心位置区域监控的主要内容,如果未能解决你的问题,请参考以下文章

CLLocationManager 区域监控与重大位置变化

后台模式下的 iOS 区域监控

设备关闭时的区域监控

CLLocation 区域监控已启动,但没有在 iPhone 上调用 didExitRegion

iOS CLLocationManager - 手动进行区域监控

在地理围栏中多次调用区域监控方法