gpt4 book ai didi

ios - startMonitoringForRegion 与 CLRegion :containCoordinate

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:29:48 24 4
gpt4 key购买 nike

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

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

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

 (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 之间的半径。因为我不在乎查看消息需要多长时间(就像我不在乎进入该区域时看到消息但我确实在乎如果我从该区域经过一次则在后面看到)我可以吗使用较小的半径?我对半径 50 米或更小的东西感兴趣? (在某些地区,我的情况甚至需要 20m)。

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

伪代码是这样的:

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

然后在委托(delegate)方法中:

- (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 个区域吗?

提前致谢。

最佳答案

1.

我怀疑第二个(didUpdateToLocation:-based)实现比第一个实现更昂贵(就电池生命周期而言),因为你只会运行第一个(startMonitoringForRegion:-based) 实现当且仅当设备位于您正在跟踪的(最多 20 个)区域之一的半径内时。

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

顺便说一句,您说代码在 100 米以上的半径范围内工作正常,但是 Apple 文档说它应该在 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.

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

关于ios - startMonitoringForRegion 与 CLRegion :containCoordinate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com