【问题标题】:CoreLocation startMonitoringRegion not firing didenterregion /didexitregion delegatesCoreLocation startMonitoringRegion 未触发 didenterregion /didexitregion 代表
【发布时间】:2011-10-21 13:16:17
【问题描述】:

我似乎无法让任何 didenterregion/didexitregion 代表被解雇,被卡住了 2 天

我使用 xcode4.2 和 ios5 sdk 和位置模拟,

有没有人能顺利完成这项工作? 100m 半径是否太小?(我试过 1000 也不起作用)还是我的代码有问题

也检查了正常的位置更新和用户位置确实进入了区域。也无法在任何地方找到答案

MainViewController(也是CLLocationManagerDelegate)的ViewDidLoad方法:

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView];
MKCoordinateRegion region;// = {{0.0,0.0},{0.0,0.0}};
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
CLLocationCoordinate2D coordinates =CLLocationCoordinate2DMake(1.34537, 103.84515);
    region.span = span;
region.center = coordinates;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

if (locationManager==nil){
locationManager = [[CLLocationManager alloc] init];
}
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
self.locationManager.delegate =self;    

if ([CLLocationManager regionMonitoringAvailable]){
    NSLog(@"regionMonitoring available");

CLRegion* region3 = [[CLRegion alloc] initCircularRegionWithCenter:coordinates radius:100 identifier:@"region3"];
[self.locationManager startMonitoringForRegion:region3 desiredAccuracy:1.0];    
NSLog(@"region = %@",region3);
NSLog(@"loc max = %f",self.locationManager.maximumRegionMonitoringDistance);
NSLog(@"location manger monitored regions = %@",self.locationManager.monitoredRegions);

还实现了所有必要的委托方法

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didfailwitherror");
}


- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
        NSLog(@"latitude %+.10f, longitude %+.10f\n",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
 }
-(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");}

感谢您的帮助

【问题讨论】:

  • 我也在处理这个问题,但运气不好。我还没有尝试过,但可能是当你从区域外开始时才调用 didEnterRegion ,然后进入?

标签: iphone core-location


【解决方案1】:

您如何测试该位置?这些区域有点棘手,但一旦设置好,它们通常工作得很好。我通常用来测试该区域的方法只是使用模拟器中的自定义位置。使用与您添加区域相同的坐标。 1000M相当大。我已经测试了小到 15-20M 的区域。如果你有正确的坐标,那应该没问题。尝试在 Apple HQ 和您的自定义坐标之间来回切换。如果您将 didEnter 和 didExit 方法注销,您应该能够几乎立即看到它们。

您可能遇到的一个问题是回调可能无法到达您的方法。在我将 AppDelegate 设置为 CLLocationManagerDelegate 之前,我的所有区域都没有工作。应用程序委托最终获得了更新并正确处理了它们。我会先尝试在那里添加这些回调,然后再回到你的主视图控制器。希望这会有所帮助。

更新

自从回答了这个问题后,我对区域监控有了更多了解。尽管您默认添加了区域,但区域会以最小区域大小添加。一位 Apple 工程师告诉我,最小区域大小为 100M。我仍然觉得它们非常准确,但它可能有助于理解为什么它会检查您。如果您需要优于 100M 的精度,您可能需要查看其他选项。

【讨论】:

  • 我遇到了同样的问题,didEnterRegion 和 didExitRegion 方法没有触发。有什么线索吗?我设置了一个半径为 1000 的区域。
  • iOS 模拟器 5.1 有一个错误,不允许触发区域监控事件。它有一个出色的雷达。如果您有权访问并且应该允许区域监控事件正常工作,您可以下载 iOS 5 模拟器或 iOS 6 模拟器。
  • 感谢@BillBurgess 的精彩回答。为了清楚起见,您是说您在 AppDelegate 中实现了 didEnterRegion 和其他?哇。我以为委托就是委托……我遇到了同样的麻烦 - 我会尝试移动函数调用。
  • 我最初在 AppDel 中实现委托只是为了让事情变得更简单,但后来将它们移到了处理所有位置委托方法的自定义类中。所以不,如果你不想的话,你不必在 AppDel 中实现它们。刚开始时它对我来说效果更好,直到我完全理解发生了什么。
  • 再次感谢@BillBurgess。虽然任何东西都可以成为代表更有意义,但我希望它能解决我的problem
猜你喜欢
  • 2013-12-13
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
相关资源
最近更新 更多