我正在尝试创建地理围栏并使用CLLocationManager和startMonitoringForRegion来监视它。问题是,我的代理不仅没有调用(是的,我实现了所有方法),但该区域没有存储在[locationManager moniteredRegions]集中。运行以下代码后的日志显示:
2012-07-31 13:46:54.208 GeofencingTest [2357:f803]创建地区:
是的 - 我检查过regionMonitoringEnabled和regionMonitoringAvailable。 不 - 我没有在设备上试过这个,只有模拟器。 是的 - 通过[locationManager startUpdatingLocation]和CLLocation * location = locationManager.location,我可以从CLLocationManager获取位置更新;
请帮忙! :)
LocationDelegate.m
- (void) createGeofence:(NSString *)identifier withCenter:(CLLocationCoordinate2D)center withRadius:(CLLocationDistance) radius{
if ([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable]){
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:radius identifier:identifier];
NSLog(@"should be succesful");
[locationManager startMonitoringForRegion:region];
}
NSLog(@"Created regions:");
for (CLRegion *my_region in [locationManager monitoredRegions]){
NSLog(@" Region: %@", my_region.identifier);
}
}
- (id) init{
self = [super init];
if (self){
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
SomeOtherClass.m
- (IBAction)createGeofence:(UIButton *)sender{
float latitude = [latitudeField.text floatValue];
float longitude = [longitudeField.text floatValue];
CLLocationDistance radius = [radiusField.text floatValue];
CLLocationAccuracy desiredAccuracy = [self getAccuracyConvertedToMeters];
[self.locationManagerObject createGeofence:identifierField.text
withCenter:CLLocationCoordinate2DMake(latitude, longitude)
withRadius:radius
withAccuracy:desiredAccuracy];
}
答案 0 :(得分:0)
当您记录创建的区域时,可能还没有监视该区域。我认为你应该尝试调用区域监控的委托方法,并在那里查找受监控的区域。
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Start monitoring for region: %@", region.identifier);
for (CLRegion *my_region in [manager monitoredRegions]){
NSLog(@"Region: %@", my_region.identifier);
}
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(@"Error: %@", error);
}