xcode iphone CLLocationmanager 区域监控回调未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8586803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:48:01  来源:igfitidea点击:

iphone CLLocationmanager Region monitoring callbacks not triggered

iphonexcodecore-locationregion

提问by tzl

I am trying to use monitoring regions to track if users have visited landmarks. the location manager is initialized in a viewcontroller along with a mapkit

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

in viewdidload of the view controller:

在视图控制器的 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);
} 

i get the NSLogs "region monitoring okay" and all the regions correctly.

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

adding of the regions are done like so

区域的添加是这样完成的

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];

but the callbacks are all not triggered

但回调都没有触发

 - (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);
}

updating the location however, works fine.

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

[locationManager startUpdatingLocation];

triggers the callback didUpdateToLocation as expected

按预期触发回调 didUpdateToLocation

Update: used didUpdatToLocation to monitor for regions instead. still interested to know why this would not work though, looks like few have had success with region monitoring

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

采纳答案by chadbag

the region tracking stuff is for low granularity position tracking and is triggered on cell location boundaries, so if you don't cross a cell boundary, you will never get your regions checked. I had the same issues and researched this and a different website had a comment about this which pointed to this apple forum:

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

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

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

If you read all the comments you will understand why this is not working.

如果您阅读了所有评论,您就会明白为什么这不起作用。

I am trying a work around where I define my own NSSets to contain tracked CLRegions and occupied CLRegions and then when I get a locationManager:didUpdateToLocation:fromLocation: callback, I check all the regions in my tracked set to see if I was NOT in the inRegions set but now am in the tracked region (add to inRegions and call back with enterRegion) or if I was inRegion but now am not (remove from inRegions and call back with exitRegion). It is a work in progress now.

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

回答by Dave Cole

You also need NSLocationAlwaysUsageDescription in your plist, and need to call [locationManager requestAlwayAuthorization] in your app. NSLocationWhenInUseUsageDescription will turn off region monitoring.

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

回答by SentineL

did you set CLLocationManagerDelegateto your ViewController?

你设置CLLocationManagerDelegate为你的 ViewController 吗?

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

回答by Edson Araujo

Try use the Debug -> Location -> Freewaydrive for simulation, I have a similiar problem, but I created a few regions and the didEnterRegion is trigged with more than 1.000 meters... I don't know Way.. I have set:

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

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

回答by Bill Burgess

What are you putting for your center coordinate? That part is missing from your example. I have seen other examples where they are are not filling it with a precise enough coordinate combined with the radius of the region for it to ever trigger. You have to have a pretty precise coordinate entered (probably 5 or 6 decimals) if you want to trigger at a 20 meter radius.

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

Everything else looks pretty good.

其他一切看起来都不错。