startMonitoringForRegion实际上有用吗?

时间:2012-01-29 12:05:04

标签: ios ios5 cllocationmanager

我一直在尝试使用startMonitoringForRegion,但遇到了捕获进入/退出事件的问题。当我在模拟器上启动应用程序并移动到我指定的位置时,我得到1个输入事件,但输入从未再次触发的事件。如果我做得对,有人可以告诉我吗?

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

2 个答案:

答案 0 :(得分:2)

我一直在我的应用程序中使用区域监控来实现地理围栏功能大约6个月,我发现它非常精确。一旦正确连接了一切,它就可以用来很好地跟踪进入和退出事件。

虽然您可以从-didUpdateToLocation获得更好,更精确的读数,但您必须权衡电池寿命才能获得它。如果您只需要偶尔更新位置,那应该没问题。如果您需要对特定位置进行持续监控,则可以采用区域监控。

我发现-startMonitoringForSignificantLocation根本不准确,不太实用。它完全依赖于细胞塔过渡和三角测量。由于这个原因,它也不能用于在模拟器中进行测试。希望这些信息可以帮到你。

答案 1 :(得分:0)

是的它有效,但它对当前时刻非常有吸引力。 (我在俄罗斯测试过)

我建议你改用它:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;