1

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

}

4

1 回答 1

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);
}
于 2013-05-18T10:14:08.100 回答