【发布时间】: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