【问题标题】:iphone CLLocationmanager Region monitoring callbacks not triggerediphone CLLocationmanager 区域监控回调未触发
【发布时间】:2012-01-25 02:05:03
【问题描述】:

我正在尝试使用监控区域来跟踪用户是否访问过地标。 位置管理器与 mapkit 一起在 viewcontroller 中初始化

在视图控制器的 viewdidload 中:

if (self.locationManager == nil)
{
    //        NSLog(@"creating location manager");
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    locationManager.distanceFilter = kCLDistanceFilterNone;
}


NSSet* set=[locationManager monitoredRegions];

if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) {
    NSLog(@"region monitoring okay");
    NSLog(@"monitored regions: %@",set);
} 

我得到了 NSLogs“区域监控正常”和所有区域正确。

区域的添加是这样完成的

double metres=20.0;
CLLocationDistance dist=metres;
CLLocationAccuracy acc=1.0;

CLRegion *reg=[[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:dist identifier:landmarkObj.landmarkName];

[locationManager startMonitoringForRegion:reg desiredAccuracy:acc];

但是回调都没有触发

 - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Entered" 
                                                    message:region.identifier
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exited" 
                                                    message:region.identifier
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    NSLog(@"started monitring for region: %@",region);
}

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

但是,更新位置可以正常工作。

[locationManager startUpdatingLocation];

按预期触发回调 didUpdateToLocation

更新:改为使用 didUpdatToLocation 来监控区域。 仍然有兴趣知道为什么这不起作用,看起来很少有人在区域监控方面取得成功

【问题讨论】:

  • 您找到解决此问题的方法了吗?我遇到了同样的问题,didStartMonitoringForRegion 没有触发。
  • 事实证明,区域监控并没有给我所需的精度。我最终对每个 didUpdateToLocation 回调进行了手动检查。这对我来说是可行的,因为我只跟踪 10 个地区

标签: iphone xcode core-location region


【解决方案1】:

区域跟踪功能用于低粒度位置跟踪,并在单元格位置边界上触发,因此如果您不跨越单元格边界,您将永远无法检查您的区域。我遇到了同样的问题并对此进行了研究,另一个网站对此发表了评论,指向了这个苹果论坛:

https://devforums.apple.com/message/251046#251046

如果你阅读了所有的 cmets,你就会明白为什么这不起作用。

我正在尝试解决我定义自己的 NSSet 以包含跟踪的 CLRegions 和占用的 CLRegions 的工作,然后当我得到 locationManager:didUpdateToLocation:fromLocation: 回调时,我检查跟踪集中的所有区域以查看我是否不在 inRegions 集中,但现在在跟踪区域中(添加到 inRegions 并使用 enterRegion 回调),或者如果我在 inRegion 但现在不在(从 inRegions 中删除并使用 exitRegion 回调)。这是一项正在进行中的工作。

【讨论】:

  • 不,情况并非如此,因为我在同一个小区运营商上有两个不同的设备,其中区域在一个上工作,而不是另一个
  • 嗯,是的,就是这样。这可能不是完整的情况,因为可能涉及其他问题,但它围绕着手机信号塔的覆盖范围和改变手机以及类似的东西来触发。它通常不使用 GPS,因为 GPS 价格昂贵。它可能有很多细微差别,边缘案例和其他东西,比如 wifi 和涉及的东西。阅读上面的苹果链接以更好地了解该问题。
【解决方案2】:

您是否将 CLLocationManagerDelegate 设置为您的 ViewController?

@interface Maps : UIViewController <CLLocationManagerDelegate>{  
...
}

【讨论】:

  • 是的,我做了@interface TrailViewController : UIViewController
  • 这实际上无关紧要。不将协议放在声明中只会影响编译器警告和内容,但不会影响实际的委托调用回您的委托。
【解决方案3】:

你的中心坐标是什么?您的示例中缺少该部分。我见过其他例子,他们没有用足够精确的坐标和区域的半径来填充它以使其永远触发。如果要在 20 米半径范围内触发,则必须输入非常精确的坐标(可能是小数点后 5 或 6 位)。

其他一切看起来都不错。

【讨论】:

  • 我使用 6 位小数。最让我困惑的是没有触发 didStartMonitoringForRegion :S
  • 我会尝试更大的半径开始,看看是否有帮助。将所有内容移入 AppDelegate 后,我才能使区域正常工作。然后我得到了我需要的所有回调。
【解决方案4】:

尝试使用 Debug -> Location -> Freeway 驱动器进行模拟,我有一个类似的问题,但是我创建了几个区域,并且触发了 didEnterRegion 超过 1.000 米...我不知道Way..我已经设置了:

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;

【讨论】:

    【解决方案5】:

    您的 plist 中还需要 NSLocationAlwaysUsageDescription,并且需要在您的应用中调用 [locationManager requestAlwayAuthorization]。 NSLocationWhenInUseUsageDescription 将关闭区域监控。

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2012-03-22
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多