startMonitoringForRegion vs CLRegion:containsCoordinate

gho*_*der 2 mkmapview ios geofencing clregion

在我的IOS应用程序中,我正在实现地理围栏.在当前的实现中,我使用的代码如下:

  CLRegion* region3 = [[CLRegion alloc] initCircularRegionWithCenter:coordinates radius:100 identifier:@"region3"];
[self.locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyHundredMeters];
Run Code Online (Sandbox Code Playgroud)

然后我使用这些委托方法:

 (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
        NSLog(@"didenterregion");

    }
    (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
        NSLog(@"didexitregion");
    }

    (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
    {NSLog(@"monitoringDidFailForRegion");}
Run Code Online (Sandbox Code Playgroud)

但是,此代码仅适用于大于100米的半径.

以下是一些问题:

  1. Apple称,在ios6及以上版本中,4s及以上的设备支持1到400m之间的半径.因为我不在乎需要多长时间才能看到这条消息(就像我不想在进入该地区时看到这条消息,但如果我从该地区过去过一次,我确实想看到后者)我可以使用较小的半径?我对50米半径或更小的东西感兴趣?(在某些地区,我的情况甚至需要20米).

我也想到了.Apple表示可以支持多达20个地区.像这样的溶剂有什么优点/缺点(我还没有实现,但我想要你的意见).

伪代码将是这样的:

Declare the regions - save them in an array
Do not call start monitoring
Run Code Online (Sandbox Code Playgroud)

然后在委托方法中:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
      for loop in all my regions {
         if ([region containsCoordinate: newLocation.coordinate])
            code for entering region
      } 
}
Run Code Online (Sandbox Code Playgroud)
  1. 它会慢吗?
  2. 它会消耗更多的电池吗?(我认为对地区进行监测并不耗电)?
  3. 它会更准确吗?
  4. 我没有注册监视器,我可以拥有超过20个地区吗?

提前致谢.

Mic*_*ann 5

1.

我怀疑第二个(didUpdateToLocation:基于)的实现比第一个实现更昂贵(就电池寿命而言),仅仅因为你只会在第一个(startMonitoringForRegion:基于)的实现中运行代码,当且仅当设备进入您正在跟踪的(最多20个)区域之一的半径.

而在第二个实现中,每次有一个" didUpdateToLocation:"委托调用(这将经常发生),代码必须运行,然后委托方法中的代码将运行.

顺便说一句,你说代码在半径100米以上的情况下运行良好,但是苹果文档说它应该在iOS6中工作,"4s及以上的设备支持1到400m之间的半径".

您的"100m"号码是您的实际结果还是您使用的设备的限制(比iPhone 4s或旧iOS版本更旧)?

2.

在后台执行任何操作都会消耗电池,但Apple已针对此进行了优化CoreLocation(假设您在应用的info.plist文件中设置了正确的标志)

3.

我认为两者都同样准确,除了"startMonitoringForRegion:"报告该区域已进入或退出可能需要几分钟的事实.

4.

是的,在第二个实现中,您可以拥有任意数量的区域.但是,您在后台运行的代码越多,电池越热,您更有可能更快地耗尽电池.