【发布时间】:2012-01-29 12:05:04
【问题描述】:
我一直在尝试使用 startMonitoringForRegion,但在捕获进入/退出事件时遇到了问题。当我在模拟器上启动应用程序并移动到我指定的位置时,我得到 1 个输入事件,但输入事件不再触发。如果我做得正确,有人可以告诉我吗?
测试.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");
}
【问题讨论】:
-
我发布了关于您关于区域监控的问题的答案。查看您的代码,以一公里的精度监视 1000M 的区域不会给您带来很好的结果。由于区域监控在电池寿命方面非常好,我会尝试使用最佳精度或最近的 10 米。它应该会提高您的准确性和您对区域监控的看法。
-
我使用 kCLLocationAccuracyBest 将我的代码更改为 10M 半径,并尝试使用模拟器触发进入/退出事件,但结果没有改变。您能否发布您的代码如何使用 startMonitoringForRegion?
-
如果您没有触发事件,您需要确保您的代理连接正确。在您发布的代码中一切看起来都不错,但我建议将回调放在您的应用程序委托中。这就是获取位置管理器回调的原因。从那里您可以将它们传递给您的其他班级,或者直接在那里处理它们。我也会从 30-40M 开始,然后从那里开始。
-
嗯...我仍然无法触发事件。你如何在 iPhone 模拟器上进行测试?
-
在调试中 --> 位置。我通常在 Apple 位置和自定义位置(这是我的区域坐标)之间来回切换。
标签: ios ios5 cllocationmanager