当前位置: 首页 > 知识库问答 >
问题:

从未调用过LocationManager的didEnterRegion

蒋联
2023-03-14

我已经厌倦了发现这个问题,但无法理解为什么没有调用Location Manager的委派方法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个答案

杜凯
2023-03-14

viewDidLoad中,添加以下代码

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

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

阎裕
2023-03-14

只有在一般设置中启用了后台应用程序刷新后,我才能获得didEnterZone和didExitZone。

韦昊焜
2023-03-14

您的头文件是否声明为CLLocationManagerDelegate?

设置监控区域后,应通过删除该代码元素进行测试,然后在按如下方式启动时检查UIApplicationLaunchProctionsLocationKey键:

 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

        NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
        if(locman == nil){
            locman = [[CLLocationManager alloc]init];


        locman.delegate = self;
        locman.distanceFilter = kCLLocationAccuracyBest;
        }

    }

然后将调用location manager代表,并且didEnter和didExit应触发ok。

 类似资料:
  • CLLocationManager 是使用 Swift 编写的,使用 Apple 和 Google 服务进行位置更新,地理编码和反向地理编码。  

  • 我想在我的android设备收到推送通知时执行一些代码。我有以下文件:、。 在中,我启动消息服务: 并且在中,我重写了方法,如下所示: 此外,在我的中,我在中有以下设置: 然后我触发通知,它出现在我的设备上(它震动,我看到通知的标题): 但是,我在中的代码从来没有执行过。 我注意到中的日志也没有被调用,所以我假设整个类都被完全忽略了。 我做了很多研究,有很多类似的问题,但我发现的所有修复,如关于清

  • 问题内容: 我有简单的代码: 它工作正常,但即使我从服务器返回404(未找到),也永远不会调用错误函数…在这种情况下,它会调用状态为404的“成功”函数… 那是对的吗? 谢谢 提琴手: 问题答案: 问题出在您的Web服务器上,它将content-length设置为0,这意味着这是一个有效值,如您在HTTP / 1.1规范中 所见。 我还在JSFiddle上举了一个例子,展示了错误和成功的例子。看这

  • 我想通过Android LocationManager API使用fused location provider,因为: 我不想让应用程序Phonelocator依赖于google play服务 现有的实现已经在工作和测试 看起来这是可能的。只需将字符串“融合”作为安装了google play服务的手机上的位置提供程序传递即可。 我想选择使用优先级高精度配置文件。我怀疑,默认情况下,它没有被使用。

  • 问题内容: 我正在寻找一种与Mockito进行验证的方法,即在测试过程中与给定的模拟没有任何交互。对于具有验证模式的给定方法,很容易实现这一点,但是我还没有找到完整模拟的解决方案。 我真正想要实现的是:在测试中验证,没有任何内容打印到控制台上。jUnit的总体思路如下: A 有很多方法,我真的不想用单独的验证方法来验证每个方法,而… 因此,我希望,如果有一个简单的解决方案,鉴于我具有良好的测试覆盖

  • 问题内容: 我有以下代码: 从不调用completionHandler函数。我尝试在浏览器中调用该URL,但工作正常。我尝试使用另一个URL,但仍然无法正常工作。我检查了一下我的ios模拟器是否可以连接到Internet,确实可以。 我不知道为什么不调用该函数,并且由于我没有任何错误,因此很难调试。 问题答案: 该任务永远不会完成,因为它永远不会开始。您必须使用其方法手动启动数据任务。