- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 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 的半径。
这里有一些问题:
我也这么认为。苹果表示最多可以支持 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.
我怀疑第二个(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.
4.
是的,在第二个实现中,您可以拥有想要跟踪的任意多个区域。但是,您在后台运行的代码越多,电池就会变得越热,并且您越有可能更快地耗尽电池电量。
关于ios - startMonitoringForRegion 与 CLRegion :containCoordinate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679188/
在我的 IOS 应用程序中,我正在实现地理围栏。在当前的实现中,我使用的代码如下: CLRegion* region3 = [[CLRegion alloc] initCircularRegion
我是一名优秀的程序员,十分优秀!