CoreLocation startMonitoringRegion没有触发didenterregion/didexitregion委托

hen*_*lee 1 iphone core-location

我似乎无法让任何didenterregion/didexitregion代表解雇,被困2天就可以了

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

有没有人有幸得到这个工作?是一个100米半径太小?(香港专业教育学院尝试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);
Run Code Online (Sandbox Code Playgroud)

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

- (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");}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Bil*_*ess 6

你是如何测试这个位置的?这些地区有点棘手,但一旦建立起来,它们通常效果很好.我通常用来测试区域只是使用模拟器中的自定义位置.使用您添加区域的相同坐标.1000M非常大.我已经测试过15-20M的小区域.如果你有正确的坐标,它应该没问题.尝试从Apple HQ和您的自定义坐标来回切换.如果你已经注销,你应该能够看到didEnter和didExit方法几乎立刻就行了.

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

UPDATE

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