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

Posted

技术标签:

【中文标题】CLLocation 区域监控已启动,但没有在 iPhone 上调用 didExitRegion【英文标题】:CLLocation region monitoring started but didExitRegion does not get called on iPhone 【发布时间】:2012-05-26 04:52:00 【问题描述】:

我允许用户设置一个位置,然后创建一个区域并监控用户是否退出或进入了该区域。我还不断监控用户与所选位置的距离。 区域监控被调用,距离被正确更新,但从不调用进入和退出。这表明我要走 30 米,但我走得更远,结果相同。

-(void)setPlugLocation:(CLLocation *)plugLocation
if (!_plugLocation) 
    _plugLocation=plugLocation;
    if(!regionManager) regionManager = [[CLLocationManager alloc] init];
    [regionManager setDelegate:self];

    CLLocationCoordinate2D plug2D;
    plug2D.latitude=plugLocation.coordinate.latitude;
    plug2D.longitude=plugLocation.coordinate.longitude;
    CLRegion *plugHome = [[CLRegion alloc] 
                            initCircularRegionWithCenter:plug2D 
                            radius:20 
                            identifier:@"Your Plug"];
    [regionManager startMonitoringForRegion:plugHome desiredAccuracy:kCLLocationAccuracyBest];



debug output:
2012-05-25 21:28:29.921 TestLocation[4412:707]  locationManager didUpdateToLocation
2012-05-25 21:28:29.929 TestLocation[4412:707] Distance from plug=0.000000
2012-05-25 21:28:30.700 TestLocation[4412:707]  locationManager didUpdateToLocation
2012-05-25 21:28:30.704 TestLocation[4412:707] Distance from plug=9.897339
2012-05-25 21:28:33.106 TestLocation[4412:707] Distance from plug=15.923317
2012-05-25 21:28:34.783 TestLocation[4412:707] region monitoring started
(identifier Your Plug) <+47.61713686,-122.28694184> radius 20.00m
2012-05-25 21:28:34.784 TestLocation[4412:707] region monitoring started
(identifier Your Plug) <+47.61713686,-122.28694184> radius 20.00m
2012-05-25 21:28:34.788 TestLocation[4412:707] Distance from plug=19.705339
2012-05-25 21:28:56.894 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:28:57.894 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:29:05.893 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:29:19.890 TestLocation[4412:707] Distance from plug=12.808387
2012-05-25 21:30:02.892 TestLocation[4412:707] Distance from plug=14.614380
2012-05-25 21:30:32.893 TestLocation[4412:707] Distance from plug=14.614380
2012-05-25 21:30:52.891 TestLocation[4412:707] Distance from plug=15.352976
2012-05-25 21:32:41.898 TestLocation[4412:707] Distance from plug=7.644082
2012-05-25 21:32:43.891 TestLocation[4412:707] Distance from plug=12.354503
2012-05-25 21:32:49.068 TestLocation[4412:707] Distance from plug=15.757565
2012-05-25 21:32:52.984 TestLocation[4412:707] Distance from plug=21.126129
2012-05-25 21:32:56.880 TestLocation[4412:707] Distance from plug=26.952410
2012-05-25 21:33:04.925 TestLocation[4412:707] Distance from plug=31.824801

【问题讨论】:

【参考方案1】:

你是否实现了正确的区域监控回调方法?

-didEnterRegion
-didExitRegion

关于区域监控的一些信息。它并不像你想象的那么精确。将添加一个最小区域大小。最小大小为 100M。因此,您添加的任何低于 100M 的内容都会增加到 100M。

看起来您正在输出中获取位置更新,所以我猜您只需要实现 CLLocationManagerDelegate 方法来处理区域监控。您还可以监控以确保正确添加区域。那里可能是一个错误,导致没有回调。

-monitoringDidFailForRegion

希望这会有所帮助。

【讨论】:

以上是关于CLLocation 区域监控已启动,但没有在 iPhone 上调用 didExitRegion的主要内容,如果未能解决你的问题,请参考以下文章

如何为超过 20 个区域启动MonitoringForRegion

未调用 CLLocation Manager didStartMonitoringForRegion 委托方法

标签文本上未显示 2 个 CLLocation 点之间的距离值

监视区域但应用程序被杀死时位置图标消失

区域监控 - didEnterRegion

iOS 区域监控重用项目问题