I am trying to use monitoring regions to track whether users have visited landmarks. the location manager is initialized in the view manager along with mapkit
in viewdidload of a view controller:
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 NSLogs “area monitoring is OK” and in all regions correctly.
adding areas is done like this:
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 all callbacks do not start
- (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); }
location updating, however, works great.
[locationManager startUpdatingLocation]
calls the didUpdateToLocation callback as expected
Update: used didUpdatToLocation to monitor regions. it’s still interesting to know why this will not work, it seems that some of them have been successful in monitoring the region.
source share