我正在尝试创建地理围栏并使用 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;
}
其他类.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];
}