4

I've been trying to use startMonitoringForRegion for while, but experiencing problems to capture enter/exit events. When I launch the app on simulator and moved to the location I specified, I get 1 enter event, but enter events never triggered again. Can somebody let me know if I'm doing correctly?

test.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface EWViewController : UIViewController<CLLocationManagerDelegate> 
{
    CLLocationManager *locman;
}

@end

test.m

- (void)viewDidLoad
{
    if(locman == nil)
        locman = [[CLLocationManager alloc]init];

    locman.delegate = self;
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.787359, -122.408227);
    CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:coord radius:1000.0 identifier:@"SF"];
    [locman startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyKilometer];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"ENTER");
}

- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"EXIT");
}
kengo
  • 601
  • 8
  • 19
  • I posted an answer in regards to your question about region monitoring. Looking at your code, monitoring a region of 1000M with accuracy of a kilometer will not get you very good results. Since region monitoring is so good on battery life, I would try using the best accuracy or nearest 10 meters. It should improve your accuracy and your opinion of region monitoring. – Bill Burgess Jan 29 '12 at 14:27
  • I changed my code to 10M radius with kCLLocationAccuracyBest and tried to trigger enter/exit events with the simulator, but the result didn't change. Could you post your code how you use startMonitoringForRegion? – kengo Jan 31 '12 at 10:56
  • If you aren't getting the events triggered, you need to make sure your delegate is connected correctly. Everything looks ok in your code you have posted, but I would suggest putting the callbacks in your app delegate. That is what gets the callbacks for the location manager. From there you can pass them on to your other class or just handle them right there. I would also start with 30-40M and work your way down from there. – Bill Burgess Jan 31 '12 at 13:23
  • Hmmm... I still can't get the events triggered. How do you test on the iPhone simulator? – kengo Jan 31 '12 at 18:32
  • Under Debug --> Location. I usually switch back and forth between the location Apple and Custom Location (which is my region coords). – Bill Burgess Jan 31 '12 at 19:28
  • Try moving your location manager to your appDelegate. When in the background, your EWViewController probably isn't getting the callbacks, the appDel is. Put your code there to start, get the callbacks working, then if you want, have your appDel call into your VC. – Bill Burgess Jan 31 '12 at 20:08
  • I do the same, but still not getting the events triggered. I'll try to figure out later again. – kengo Jan 31 '12 at 20:17
  • Not sure what to tell you. Try debugging when you create the region. Are you getting the new region monitor location icon? (purple outlined). Make sure you have a valid locationManager initialized, make sure your region is getting added. Try checking for any outstanding regions being monitored. [locMan monitoredRegions]. Everything looks good on my end, so you will have to add more code, or dig a little on your own. Good luck. – Bill Burgess Jan 31 '12 at 20:21

2 Answers2

2

I have been using region monitoring for a geofence feature in my app for about 6 months and I have found it to be very precise. Once you have everything wired up correctly, it can be used to track enter and exit events quite well.

While you can get even better and more precise readings from -didUpdateToLocation, you will have to trade off battery life to get it. If you only need occasional location updates, it should be fine. If you need constant monitoring for specific locations, region monitoring is the way to go.

I have found that -startMonitoringForSignificantLocation is not accurate at all and not very practical. It relies solely on cell tower transitions and triangulation. It also can't be used to test in the simulator for this very reason. Hope some of this information helps you out.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
  • I can't agree with you. `startMonitoringForSignificantLocation` is much more precisely than `startMonitoringForRegion`. And also `startMonitoringForRegion` isn't supported by iPhone 3Gs. – Shmidt Jan 29 '12 at 20:13
  • 1
    We will have to agree to disagree. I have heard nothing but issues on location for significant location changes. Region monitoring is limited because it can only monitor a select number of specific regions, it does get the added benefit of more location updates from the OS. Wifi connections, cell handoffs, other application location updates, etc. This is my experience anyway. – Bill Burgess Jan 30 '12 at 01:40
  • Probably in US it is so, but not in Europe) — that's my experience. – Shmidt Jan 30 '12 at 09:15
0

Yes it works, but it is very very unprecised for the current moment. (I tested it in Russia)

I recommend you using this instead:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;
Shmidt
  • 16,436
  • 18
  • 88
  • 136