Does startMonitoringForRegion actually work?

梦想与她 提交于 2019-12-14 03:49:05

问题


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");
}

回答1:


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;



回答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.



来源:https://stackoverflow.com/questions/9053029/does-startmonitoringforregion-actually-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!