- (voID)requestUsersCurrentLocation{ if (self.locationManager == nil) self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 100; [self.locationManager startUpdatingLocation];}
使用CLLocationManager的委托方法,我检查应用程序是否能够确定用户的当前位置.在我的测试中,这工作正常,应用程序继续调用我的attemptToRegisterGeofenceAlertForLocation:方法
- (voID)locationManager:(CLLocationManager *)manager dIDUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations lastObject]; NSDate *eventDate = location.timestamp; NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; if (abs(howRecent) < 15.0) { [self.locationManager stopUpdatingLocation]; if([self attemptToRegisterGeofenceAlertForLocation:location]) { [[NSNotificationCenter defaultCenter] postNotificationname:kGeofenceSetupSuccess object:nil]; } else { [[NSNotificationCenter defaultCenter] postNotificationname:kGeofenceSetupFailure object:nil]; } }}
到现在为止还挺好.这是我的自定义函数,用于在用户当前位置周围注册相对较小的地理围栏:
- (BOol)attemptToRegisterGeofenceAlertForLocation:(CLLocation *)location{ // Do not create regions if support is unavailable or Disabled if (![CLLocationManager regionMonitoringAvailable]) return NO; // Check the authorization status if (([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) && ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined)) return NO; // Clear out any old regions to prevent buildup. if ([self.locationManager.monitoredRegions count] > 0) { for (ID obj in self.locationManager.monitoredRegions) { [self.locationManager stopMonitoringForRegion:obj]; } } // If the overlay's radius is too large,registration fails automatically,// so clamp the radius to the max value. CLLocationdegrees radius = 100; // meters? if (radius > self.locationManager.maximumRegionMonitoringdistance) { radius = self.locationManager.maximumRegionMonitoringdistance; } // Create the region to be monitored. CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:location.coordinate radius:radius IDentifIEr:kGeofenceIDentifIEr]; [self.locationManager startMonitoringForRegion:region]; return YES;}
当用户退出地理围栏区域时,我会这样回复:
- (voID)locationManager:(CLLocationManager *)manager dIDExitRegion:(CLRegion *)region{ if([region.IDentifIEr isEqual:kGeofenceIDentifIEr]) { if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) { // Fire a UILocationNotification } else { // Fire a UIAlertVIEw } }}
我已经确定该应用程序能够获取用户位置并且地理围栏已正确注册,但是我无法在实际设备(启用3G的iPhone)上触发它.我离开了地理围栏区域并且没有收到任何通知就走了几英里.我已成功设法通过大幅改变我的位置来接收模拟器中的警报.
我究竟做错了什么?
解决方法 看起来您的半径设置为100M(好).距离过滤器也设置为100M,可能只是对你不利.在模拟器中,您可以获得非常精确的更新,因为您必须将它们传递进去.在实际测试中,您获得的Wifi逼近和细胞三角测量的坐标要小于精确坐标.我的经验是,如果我使用kCLLocationAccuracyBest进行距离过滤,我会得到非常好的结果.由于区域监控已经从系统供电不足,因此只需将滤波器设置为最佳,就不会产生电池损坏.
总结以上是内存溢出为你收集整理的ios – 永远不会调用CLLocationManager的didExitRegion全部内容,希望文章能够帮你解决ios – 永远不会调用CLLocationManager的didExitRegion所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)