6

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

in viewdidload of the 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 the NSLogs "region monitoring okay" and all the regions correctly.

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

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

tzl
  • 1,540
  • 2
  • 20
  • 31
  • Did you figure out a solution to this issue? I'm having the same issue and didStartMonitoringForRegion is not firing. – Arunabh Das Aug 06 '12 at 03:15
  • it turns out that region monitoring just does not give me the precision i need. I ended up doing a manual check for every didUpdateToLocation callback. This was feasible for me since i was tracking for only 10 regions – tzl Aug 24 '12 at 06:47

5 Answers5

3

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.

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.

chadbag
  • 1,837
  • 2
  • 20
  • 34
  • no, this is not the case, as i have two different devices on the same cell carrier in which the regions work on one, but not the other – uofc Sep 10 '14 at 19:41
  • well, yes, it is the case. It may not be the complete case, as there maybe other issues involved, but it revolves around cell tower coverage and changing cells and stuff like that to trigger. It does not usually use GPS as GPS is expensive. There are probably lots of nuances to it and edge cases and other things like wifi and stuff involved. Read the apple link above to get a better understanding of the issue. – chadbag Sep 11 '14 at 20:02
0

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

Dave Cole
  • 2,446
  • 2
  • 20
  • 26
0

did you set CLLocationManagerDelegate to your ViewController?

@interface Maps : UIViewController <CLLocationManagerDelegate>{  
...
}
SentineL
  • 4,682
  • 5
  • 19
  • 38
  • yes i did @interface TrailViewController : UIViewController – tzl Dec 21 '11 at 12:53
  • 2
    This is actually irrelevant. Not putting the protocol in the declaration only affects compiler warnings and stuff but does not affect the actual delegate calls back to your delegate. – chadbag Feb 04 '12 at 22:22
0

Try use the Debug -> Location -> Freeway drive 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:

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
0

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.

Everything else looks pretty good.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • i am using 6 decimals. what puzzles me the most is that didStartMonitoringForRegion is not being triggered :S – tzl Dec 29 '11 at 03:42
  • I would try a larger radius to start, see if that helps. I was only able to get regions working after I moved everything into my AppDelegate. Then I got all the callbacks I needed. – Bill Burgess Dec 29 '11 at 13:39