2011-12-21 55 views
6

我想使用监控区域来跟踪用户是否曾访问过地标。 外景经理是在视图控制器初始化在视图控制器的viewDidLoad中一个mapkitiphone CLLocationmanager区域监控回调未触发

沿:

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监视的区域来代替。 仍然有兴趣知道为什么这不起作用,但看起来很少有成功的区域监测

+0

您是否找出了解决此问题的解决方案?我遇到了同样的问题,并且StartStonMonitoringForRegion没有启动。 – Das 2012-08-06 03:15:58

+0

事实证明,区域监控并不能提供我需要的精度。我最终为每个didUpdateToLocation回调进行手动检查。这对我来说是可行的,因为我只跟踪10个区域 – tzl 2012-08-24 06:47:40

回答

3

区域跟踪的内容是针对低粒度的位置跟踪,并在单元格位置边界上触发,因此如果您不跨越单元边界,则永远不会检查您的区域。我有同样的问题,并研究这一点,并在不同的网站有关于这个评论其指出,这个苹果论坛:

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

如果你读你就会明白,为什么这是行不通的意见。

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

+0

不,事实并非如此,因为我在同一个单元载体上有两个不同的设备,其中的区域在一个区域上工作,但是没有其他 – uofc 2014-09-10 19:41:15

+0

好,是的,情况就是这样。这可能不是完全的情况,因为可能涉及其他问题,但它围绕着细胞塔的覆盖范围以及改变细胞和类似的东西来触发。由于GPS价格昂贵,通常不使用GPS。它可能有很多细微差别,边缘情况和其他东西,如WiFi和涉及的东西。阅读上面的Apple链接,以更好地了解问题。 – chadbag 2014-09-11 20:02:47

0

你是否设置CLLocationManagerDelegate到您的ViewController?

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

是的,我做了\t @interface TrailViewController:UIViewController tzl 2011-12-21 12:53:54

+2

这实际上是不相关的。不在协议中声明只影响编译器的警告和东西,但不影响实际的委托调用回你的委托。 – chadbag 2012-02-04 22:22:39

0

尝试使用调试 - >位置 - >高速公路驱动器模拟,我有一个类似的问题,但我创建了一些地区和didEnterRegion被触发以超过1000米... ...我不不知道方式..我已经设置:

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

你对你的中心坐标有什么作用?你的例子中缺少了这部分。我看过其他的例子,他们没有用足够精确的坐标来结合区域的半径来触发它。如果要在20米半径处触发,必须输入相当精确的坐标(可能为5或6位小数)。

其他一切看起来都不错。

+0

我使用6位小数。最令我感到困惑的是,didStartMonitoringForRegion没有被触发:S – tzl 2011-12-29 03:42:56

+0

我会尝试更大的半径来启动,看看是否有帮助。在我将所有内容都移入我的AppDelegate之后,我只能够让区域工作。然后我得到了我需要的所有回调。 – 2011-12-29 13:39:06

0

您还需要NSLocationAlwaysUsageDescription在你的plist中,并需要调用[的LocationManager requestAlwayAuthorization]在您的应用程序。 NSLocationWhenInUseUsageDescription将关闭区域监视。