【问题标题】:startMonitoringForRegion vs CLRegion:containCoordinatestartMonitoringForRegion 与 CLRegion:containCoordinate
【发布时间】:2013-09-11 19:37:47
【问题描述】:

在我的 IOS 应用程序中,我正在实施地理围栏。在当前的实现中,我使用这样的代码:

  CLRegion* region3 = [[CLRegion alloc] initCircularRegionWithCenter:coordinates radius:100 identifier:@"region3"];
[self.locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyHundredMeters];

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

 (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");}

但是,此代码仅适用于大于 100m 的半径。

这里有一些问题:

  1. Apple 表示,在 ios6 及更高版本中,4s 及更高版本的设备支持 1 到 400m 之间的半径。因为我不在乎查看消息需要多长时间(就像我在进入该区域时不关心看到消息,但如果我曾经从该区域经过,我确实关心在后者看到)我可以使用更小的半径?我对半径 50m 或更小的东西感兴趣? (在某些地区,我的情况甚至需要 20m)。

我也这么认为。苹果表示最多可以支持 20 个地区。像这样的解决方案有什么优点/缺点(我还没有实现它,但我想听听你的意见)。

伪代码是这样的:

Declare the regions - save them in an array
Do not call start monitoring

然后在委托方法中:

- (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
      } 
}
  1. 会更慢吗?
  2. 会消耗更多电池吗? (我认为监控区域不会消耗电池)?
  3. 能更准确吗?
  4. 由于我没有注册监视器,我可以拥有超过 20 个区域吗?

提前致谢。

【问题讨论】:

标签: ios mkmapview geofencing clregion


【解决方案1】:

1.

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

而在第二个实现中,code has to run each time there's a "didUpdateToLocation:" delegate call(会经常发生)然后委托方法中的代码将运行。

顺便说一句,您说代码在 100 米以上的半径范围内工作正常,但苹果文档说它应该在 iOS6 中工作,“设备 4s 支持 1 到 400 米之间的半径及以上。”

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

2.

在后台执行任何操作确实会消耗电池but Apple has optimized CoreLocation for this somewhat (provided you set the correct flag in your app's info.plist file)

3.

我认为两者都差不多,except for the fact it may take up to a few minutes for "startMonitoringForRegion:" to report that the region was entered or exited

4.

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

【讨论】:

  • 道特曼 感谢您提供所有信息!如果无论如何调用 didUpdateToLocation 中的委托代码,您有什么建议 - 因为我正在使用它来测量当前速度、距离等?或者我在里面做的事情越多,我消耗的电池就越多。是的,我确实有一部 iPhone 4,所以我猜这是设备的限制。
  • 我认为这是“我在里面做的事情越多,消耗的电池就越多”的情况。我有自己的另一个项目,我正在做后台处理,它真的会消耗电池寿命,所以它必须在短期内得到修复。客户对电池寿命等性能问题非常敏感,因此您可以采取任何措施来优化性能,这将是一件“好事”。
猜你喜欢
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2011-05-08
  • 2012-01-24
相关资源
最近更新 更多