Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 区域监控当前位置不';出口时不要通知_Ios_Objective C_Core Location_Cllocationmanager_Clregion - Fatal编程技术网

Ios 区域监控当前位置不';出口时不要通知

Ios 区域监控当前位置不';出口时不要通知,ios,objective-c,core-location,cllocationmanager,clregion,Ios,Objective C,Core Location,Cllocationmanager,Clregion,我正在尝试测试区域监控,因此我得到的当前位置如下: - (void)startLocationTracking { CLLocationManager *locationManager = [[CLLocationManager alloc] init]; // Start location manager if ([CLLocationManager locationServicesEnabled] && [CLLocationManager auth

我正在尝试测试区域监控,因此我得到的当前位置如下:

- (void)startLocationTracking
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];

    // Start location manager
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        locationManager = [LocationTracker sharedLocationManager];
        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:manager.location.coordinate radius:300 identifier:@"first location initializer"];

        NSLog(@"0: %@", manager.location);
        NSLog(@"1: %@", region);

        [manager startMonitoringForRegion:region];
        NSLog(@"[locationManager startMonitoringForRegion:%@];", region);
    });
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);

    NSArray *allRegions = manager.monitoredRegions.allObjects;

    if (allRegions.count > 0) {
        for (CLRegion *reg in allRegions) {
            [manager stopMonitoringForRegion:reg];
        }
    }

    CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(manager.location.coordinate.latitude, manager.location.coordinate.longitude);
    CLRegion *regionNew = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:300 identifier:@"new region"];

    NSLog(@"region: %@", region);
    NSLog(@"regionNew: %@", regionNew);

    [manager startMonitoringForRegion:regionNew];
}
通过区域监控跟踪第一个位置,如下所示:

- (void)startLocationTracking
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];

    // Start location manager
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        locationManager = [LocationTracker sharedLocationManager];
        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:manager.location.coordinate radius:300 identifier:@"first location initializer"];

        NSLog(@"0: %@", manager.location);
        NSLog(@"1: %@", region);

        [manager startMonitoringForRegion:region];
        NSLog(@"[locationManager startMonitoringForRegion:%@];", region);
    });
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);

    NSArray *allRegions = manager.monitoredRegions.allObjects;

    if (allRegions.count > 0) {
        for (CLRegion *reg in allRegions) {
            [manager stopMonitoringForRegion:reg];
        }
    }

    CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(manager.location.coordinate.latitude, manager.location.coordinate.longitude);
    CLRegion *regionNew = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:300 identifier:@"new region"];

    NSLog(@"region: %@", region);
    NSLog(@"regionNew: %@", regionNew);

    [manager startMonitoringForRegion:regionNew];
}
然后在当前区域的每个出口,我都会像这样监视新的位置:

- (void)startLocationTracking
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];

    // Start location manager
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        locationManager = [LocationTracker sharedLocationManager];
        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:manager.location.coordinate radius:300 identifier:@"first location initializer"];

        NSLog(@"0: %@", manager.location);
        NSLog(@"1: %@", region);

        [manager startMonitoringForRegion:region];
        NSLog(@"[locationManager startMonitoringForRegion:%@];", region);
    });
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, region);

    NSArray *allRegions = manager.monitoredRegions.allObjects;

    if (allRegions.count > 0) {
        for (CLRegion *reg in allRegions) {
            [manager stopMonitoringForRegion:reg];
        }
    }

    CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(manager.location.coordinate.latitude, manager.location.coordinate.longitude);
    CLRegion *regionNew = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:300 identifier:@"new region"];

    NSLog(@"region: %@", region);
    NSLog(@"regionNew: %@", regionNew);

    [manager startMonitoringForRegion:regionNew];
}
我将解释我预期会发生什么:

  • 在区域监视中注册当前位置
  • 通知用户退出当前区域
  • 在退出日志上,再次将当前位置注册为区域
  • 这不会发生

    我错在哪里

    我在“高速公路驾驶”模拟器上试过

    更新:


    经过测试并正常运行,由于地理围栏中存在苹果漏洞,该应用程序仅支持7.1+,非常糟糕,但我没有其他想法。

    我认为您实施区域监控的方式可能会导致一些问题

    原因如下:-

  • startLocationTracking
    方法中,您的
    locationManager
    是一个本地对象,不会扩展到该方法的生命周期。这还意味着每次调用
    startLocationTracking
    ,都会有一个新的
    locationManager
    对象,该对象被分配了一个新的内存块

    要解决此问题:您应该在应用程序的整个生命周期中使用共享的
    locationManager
    单例

  • 我认为您不应该
    在委托方法
    -(void)locationManager:(CLLocationManager*)manager didUpdateLocations:
    中启动区域监控。原因是,如果您再次调用
    startLocationTracking
    ,将有多个locationManager。多个LocationManager可以监视可能导致多个通知的同一区域

  • 调用
    [manager startMonitoringForRegion:region]后,将不会立即监视该区域。如果您不相信我,请尝试以下代码:-

    [locationManager startMonitoringForRegion:region];
    NSLog(@"%@",locationManager.monitoredRegions);
    
  • 您将发现刚才监视的区域不在
    locationManager.monitoredRegions
    内。由于这是在iOS级别处理的,因此,我认为可能需要几分钟的时间才能准备好对该地区进行监控

    您还应该了解iOS中区域监视的其他限制:-

    一个应用程序一次最多可以注册20个区域。为了报告 区域变化及时,区域监控服务 需要网络连接

    在iOS 6中,半径在1到400米之间的区域效果更好 在iPhone 4S或更高版本的设备上。(在iOS 5中,指半径为 1米到150米在iPhone4S和更高版本的设备上工作得更好。) 在这些设备上,应用程序可以预期接收到适当的区域 在上的3到5分钟内输入或区域退出通知 平均水平,如果不是更早的话

    注意:一旦设备移动500,应用程序就会收到通知 距离上次通知的距离为米或更多。它不应该期待 通知频率高于每五分钟一次。如果 设备能够从网络、位置管理器中检索数据 更有可能及时发送通知

    我不知道你的应用程序是关于什么的,我相信你应该重新设计你的应用程序的流程。您应该尝试监视委托方法之外的区域

    有关Singleton LocationManager的更多信息,您可以查看以下答案:。GitHub上有一个完整的项目,其中包含一个Singleton LocationManager类,我将其命名为LocationTracker


    您可能还想查看我一个月前发现的iOS 7中区域监控的一个小故障(有一个解决小故障的解决方法):

    对于代理方法(didEnterRegion和didExitRegion)没有被调用,最令人满意的答案是,apple docs说在进入或退出后3到5分钟会通知您,但在实际测试中,我发现您需要等待大约7到8分钟

    我测试了10到15次,我的区域半径约为80米。我挠头想看看出了什么问题。我打开模拟器,打开位置跟踪(使用gpx文件进行位置模拟)。8分钟后,Didextregion接到了电话

    另外,如果要在后台执行此操作,则必须在目标上启用后台模式。

    好吧,你不应该在模拟器上测试这种行为。你需要一个真正的设备。我试过为一个位置跟踪程序做一次,但它只是在模拟器上随机出现。经过实时测试,仍然不起作用。你能详细介绍一下你的更新吗?在没有答案中提供的解决方案的情况下,您的问题中的代码现在是否可以工作?我知道这个问题已经讨论了很长时间,但在地理围栏中,我面临着一个问题。我选择了100米半径,应该会发出“出口时”通知。但我在250米左右收到通知。请帮帮我。我知道这个问题已经讨论很久了,但在地理围栏方面,我面临着一个问题。我选择了100米半径,应该会发出“出口时”通知。但我在250米左右收到通知。请帮助我。如果我只想监控一个建筑区域,意味着我如何设置它。如果我从谷歌地图获得cordinates,它会起作用吗?向上投票!虽然我同意区域监视、SLC和访问监视都是在操作系统级别上管理的。我想已经不是你说的那样了。从iOS 10、iPhone6开始,当我执行[locationManager startMonitoringForRegion:region]时;NSLog(@“%@”,locationManager.monitoredRegions)我确实立即看到了。@UmangKothari我面临着同样的问题?你们找到解决办法了吗?我知道这个问题已经很久了,但在地理围栏方面,我面临着一个问题。我有一本书