【发布时间】: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