首页 文章

没有调用LocationManager的didEnterRegion

提问于
浏览
3

我厌倦了找到这个问题,但无法理解为什么会发生位置管理器的委托方法 didEnterRegiondidExitRegion 未被调用 . 我在我的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 结尾,在我的情况下调用,但我有 didEnterdidExit 的问题 . 我也实现了 didFailWithError 但它没有做到这一点 . 请提供任何线索,为什么不调用这两种方法 . 或者给我任何调用这些方法的链接,我没有找到这些方法的示例/来源 . 任何帮助将不胜感激 .

3 回答

  • 1

    viewDidLoad 中,添加以下代码

    self.beaconRegion.notifyOnEntry=YES;
    self.beaconRegion.notifyOnExit=YES;
    self.beaconRegion.notifyEntryStateOnDisplay=YES;
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];
    

    我希望这能解决你的问题 .

  • 0

    是您的头文件声明为CLLocationManagerDelegate?

    一旦设置了要监视的区域,就应该通过删除代码中的元素进行测试,然后在启动时检查UIApplicationLaunchOptionsLocationKey键:

    if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){
    
            NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
            if(locman == nil){
                locman = [[CLLocationManager alloc]init];
    
    
            locman.delegate = self;
            locman.distanceFilter = kCLLocationAccuracyBest;
            }
    
        }
    

    然后将调用位置管理器委托,并且didEnter和didExit应该正常启动 .

  • 0

    在一般设置中启用后台应用程序刷新后,我才能获得didEnterRegion和didExitRegion .

相关问题