2012-07-31 141 views
1

我想創建一個geofence並使用CLLocationManager和startMonitoringForRegion監視它:問題是,我的代理不僅沒有得到任何調用(是的,我實現了所有方法),但該區域沒有存儲在[locationManager moniteredRegions]集合中。運行下面的代碼後,日誌顯示:startMonitoringForRegion不創建區域

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

這可能是當您登錄創建的區域,該區域還沒有尚未被監測。我認爲你應該嘗試調用區域監測的代表方法,並在那裏尋找監測區域。

- (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); 
} 
+0

即使我將主機託管代理設置爲self,也不會爲我調用這些方法。 – coolcool1994 2014-03-13 22:20:19