iphone CLLocationmanager区域监控回调未触发 [英] iphone CLLocationmanager Region monitoring callbacks not triggered
问题描述
我正在尝试使用监控区域来跟踪用户是否访问过地标。
位置管理器在视图控制器中初始化以及视图控制器的viewdidload中的mapkit
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
:
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区域监控好和所有区域正确。
i get the NSLogs "region monitoring okay" and all the regions correctly.
添加区域就像这样做
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
triggers the callback didUpdateToLocation as expected
更新:使用didUpdatToLocation来监控区域。
仍然有兴趣知道为什么这不起作用,看起来很少有人在区域监控方面取得了成功
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
推荐答案
区域跟踪内容用于低粒度位置跟踪,并在单元位置边界上触发,因此如果不跨越单元格边界,则永远不会检查区域。我有同样的问题,并研究了这个和一个不同的网站有关于此的评论指向这个苹果论坛:
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
如果您阅读了所有评论,您将理解为什么这不起作用。
If you read all the comments you will understand why this is not working.
我正在尝试解决我定义自己的NSSets以包含跟踪的CLRegions和占用CLRegions的问题。然后当我得到一个locationManager:didUpdateToLocation:fromLocation:callback时,我检查我跟踪集合中的所有区域,看看我是不在inRegions集合中,但现在是在跟踪区域(添加到inRegions并使用enterRegion回调)或如果我在地区但现在不是(从inRegions中移除并使用exitRegion回调)。现在正在进行中。
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.
这篇关于iphone CLLocationmanager区域监控回调未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!