oli*_*erk 11 iphone monitoring core-location region
我尝试让iPhone4监控区域并通过调用didEnterRegion或didExitRegion通知我.我无法让它发挥作用.我正在阅读这里所有相关的enries,再加上网上的几篇文章...... iOS只是不调用我的CLLocationManagerDelegate方法.我做了什么:
我有一个简单的AppDelegate,它还实现了didEnterRegion和didExitRegion的CLLocationManagerDelegate方法.在这些方法中,我只需使用UILocalNotification来报告事件.从ViewController我创建一个区域(当前位置),其中一个瑞士值为1000米.
cdu*_*uhn 43
以下是一些要检查的事项:
在开始监控代码中的区域之前,请致电[CLLocationManager regionMonitoringAvailable]
并[CLLocationManager regionMonitoringEnabled]
确保该服务在用户的电话上可用并启用.
确保将位置管理器的delegate
属性设置为您已实现的对象locationManager:didEnterRegion:
和/或locationManager:didExitRegion:
.
确保这些方法签名中没有任何拼写错误.小的大写错误会导致这些消息的传递失败.将这些复制/粘贴到您的代码中并确保它们匹配:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{ /* Handle enter */ }
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{ /* Handle exit */ }
Run Code Online (Sandbox Code Playgroud)确保您的代理也实现locationManager:monitoringDidFailForRegion:withError:
,因为它可能会告诉您服务失败的原因.
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Region monitoring failed with error: %@", [error localizedDescription]);
}
Run Code Online (Sandbox Code Playgroud)可能发生此类监视失败的一个原因是Core Location对允许应用程序监视的区域数量施加限制.实际上,这个限制似乎是每个应用程序大约十个区域.因此,请确保删除不需要使用的区域stopMonitoringForRegion:
,并仅按照Apple的" 位置感知编程指南"的建议监控最靠近用户的区域:
在指定要监视的区域集时,您应始终谨慎.区域是共享系统资源,系统范围内可用区域的总数是有限的.因此,Core Location限制了单个应用程序可以同时监视的区域数量.要解决这些限制,您应该考虑只注册用户附近的那些区域.随着用户位置的变化,您可以删除现在更远的区域并添加用户路径上的区域.如果您尝试注册区域并且空间不可用,则位置管理器
locationManager:monitoringDidFailForRegion:withError:
使用kCLErrorRegionMonitoringFailure
错误代码调用其委托的 方法.
希望显而易见,但请确保startMonitoringForRegion:desiredAccuracy:
在设置代表后进行调用.
初始化CLRegion
要监视的对象时,请initCircularRegionWithCenter:radius:identifier:
确保为每个区域使用唯一标识符.
如果你locationManager:didEnterRegion:
和locationManager:didExitRegion:
方法越来越正常调用时,应用程序是活动的,而不是在操作系统将重新启动您的应用程序在它被杀害后的背景,那么你可能无法正确初始化您的位置经理,在这种情况下,设置其委托.如果您在应用程序未运行时跨越已注册的区域边界,操作系统将在后台启动您的应用程序,您可以使用应用程序委托if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]]) {}
的application:didFinishLaunchingWithOptions:
方法检测该应用程序.您的应用程序在后台启动时可能不会加载任何视图,因此您需要确保application:didFinishLaunchingWithOptions:
调用一些代码路径来实例化您的位置管理器对象并在此情况下设置其委托.只要您的位置管理员的委托属性设置完毕,就会传递任何待处理的区域监控事件.
同样在这里。我也在尝试获取 didEnterRegion 并通知我。我看到的是该方法永远不会被调用。所以我正在进行自定义签入(newLocation,oldLocation 函数)
分配距离并且:
{
distance = [newLocation distanceFromLocation:(your location)];
if(distance < 300){
[appDelegate Notify:@"Entering "];
}
Run Code Online (Sandbox Code Playgroud)
重大位置变化很糟糕!
归档时间: |
|
查看次数: |
9305 次 |
最近记录: |