![](/img/trans.png)
[英]Methods LocationManager:didEnterRegion and LocationManager:didExitRegion is getting called continuously
[英]didEnterRegion of LocationManager not called ever
我厌倦了发现这个问题,但无法理解为什么会发生这种情况,因为位置管理器的委托方法didEnterRegion
或didExitRegion
被调用过。 我在我的 iPhone 上对此进行了测试,但它不起作用。 请告诉我我错过了什么。 我的源代码是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; manager = [[CLLocationManager alloc] init]; manager.delegate = self; dist = 100; coord = CLLocationCoordinate2DMake(24.004686, 74.554088); region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"]; [manager startMonitoringForRegion:region desiredAccuracy:10]; return YES; } - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"Enter Region."); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"didExitRegion"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Exited the Location." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; // [alert release]; }
任何人都可以弄清楚出了什么问题吗? 我搜索了很多演示和源代码,但其中大部分都以didUpdateToLocation
,在我的案例中调用了它,但我对didEnter
和didExit
有疑问。 我也实现了didFailWithError
但它没有做这件事。 请给出为什么不调用这两种方法的任何线索。 或者给我任何调用这些方法的链接,我没有找到这些方法的示例/源代码。 任何帮助将不胜感激。
您的头文件是否声明为 CLLocationManagerDelegate?
一旦你设置了监控区域,你应该通过删除该代码元素进行测试,然后在启动时检查 UIApplicationLaunchOptionsLocationKey 键,如下所示:
if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){
NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
if(locman == nil){
locman = [[CLLocationManager alloc]init];
locman.delegate = self;
locman.distanceFilter = kCLLocationAccuracyBest;
}
}
然后将调用位置管理器委托,并且 didEnter 和 didExit 应该正常触发。
在viewDidLoad
中,添加以下代码
self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];
我希望这会解决你的问题。
只有在常规设置中启用后台应用程序刷新后,我才能获得 didEnterRegion 和 didExitRegion。
您是否也尝试过didDetermineState
? didEnterRegion
和didExitRegion
仅在进入/退出时调用,但根据我的理解,如果您在该地区并且实际上没有移动, didDetermineState
会告诉您您当前是否在该地区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.