2012-02-16 59 views
2

我在模擬器和設備上都遇到iOS 5中CoreLocation區域委託方法的問題。我正在嘗試添加一個用於監視的區域,然後等待didStartMonitoring委託回調。很少,它工作正常。但是,通常不會調用didStartMonitoringmonitoringDidFail。該地區確實被添加到monitoredRegions。委託對象設置正確,通常會撥打didEnterRegiondidExitRegion。位置管理員從未被釋放。這是在main thread。我檢查了我能想到的所有條件。CoreLocation區域代理不叫

-(id) init 
{ 
    self = [super init]; 
    if(self) { 
     NSLog(@"initializing location manager"); 
     self.locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     [locationManager startUpdatingLocation]; 
    } 
    return self; 
} 

-(void) startMonitoringRegion 
{ 
    BOOL monitoring = NO;  
    if ([CLLocationManager regionMonitoringAvailable]) { 
     if ([CLLocationManager regionMonitoringEnabled]) { 
     if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) { 
      monitoring = YES; 
     } else { 
      NSLog(@"app is not authorized for location monitoring"); 
     } 
     } else { 
     NSLog(@"region monitoring is not enabled"); 
     } 
    } else { 
     NSLog(@"region monitoring is not available"); 
    } 
    if(!monitoring) return; 

    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate 
                  radius:50 
                 identifier:@"majorRegion"]; 
    NSLog(@"trying to start monitoring for region %@", region); 
    [locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest]; 
} 

-(void)  locationManager:(CLLocationManager*)manager 
didStartMonitoringForRegion:(CLRegion*)region 
{ 
    NSLog(@"region monitoring started"); 
} 

- (void) locationManager:(CLLocationManager *)manager 
monitoringDidFailForRegion:(CLRegion *)region 
       withError:(NSError *)error 
{ 
    NSLog(@"region monitoring failed"); 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"location manager failed"); 
} 

任何人有什麼想法?我可以處理這個問題,但看起來didEnterRegiondidExitRegion委託方法有時也不一致,這對我來說是個大問題。

編輯:我可以在一個應用程序委託中複製這個功能 - 沒有自定義對象或任何東西。見下面的實現。區域被添加(打印時看到),但didStartMonitoringRegion永遠不會被調用。

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize locationManager; 

-(void) startMonitoringRegion 
{ 
    BOOL monitoring = NO;  
    if ([CLLocationManager regionMonitoringAvailable]) { 
     if ([CLLocationManager regionMonitoringEnabled]) { 
     if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) { 
      monitoring = YES; 
     } else { 
      NSLog(@"app is not authorized for location monitoring"); 
     } 
     } else { 
     NSLog(@"region monitoring is not enabled"); 
     } 
    } else { 
     NSLog(@"region monitoring is not available"); 
    } 
    if(!monitoring) return; 

    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:locationManager.location.coordinate 
                   radius:50. 
                  identifier:@"majorRegion"]; 
    NSLog(@"trying to start monitoring for region %@", region); 
    [locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest]; 
} 

-(void) printMonitoredRegions 
{ 
    NSLog(@"printing regions:"); 
    for(CLRegion* region in locationManager.monitoredRegions) 
     NSLog(@"%@", region); 
} 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSLog(@"initializing location manager"); 
    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation]; 

    [self startMonitoringRegion]; 
    [self performSelector:@selector(printMonitoredRegions) withObject:nil afterDelay:2.]; 

    return YES; 
} 


- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    //NSLog(@"location updated"); 
} 

-(void)  locationManager:(CLLocationManager*)manager 
didStartMonitoringForRegion:(CLRegion*)region 
{ 
    NSLog(@"region monitoring started"); 
} 

-(void) locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region 
{ 
    NSLog(@"did enter region"); 
} 

-(void) locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region 
{ 
    NSLog(@"did exit region"); 
} 

- (void) locationManager:(CLLocationManager *)manager 
monitoringDidFailForRegion:(CLRegion *)region 
       withError:(NSError *)error 
{ 
    NSLog(@"region monitoring failed"); 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"location manager failed"); 
} 

@end 

登錄:

2012-02-21 10:53:50.397 locationtest[64440:f803] initializing location manager 
2012-02-21 10:53:50.412 locationtest[64440:f803] trying to start monitoring for region (identifier majorRegion) <LAT,LONG> radius 50.00m 
2012-02-21 10:53:52.414 locationtest[64440:f803] printing regions: 
2012-02-21 10:53:52.416 locationtest[64440:f803] (identifier majorRegion <LAT,LONG> radius 50.00m 

編輯2:我剛剛注意到CLLocationManagerDelegate協議iOS implementationMac implementation略有不同 - 值得注意的是,蘋果並沒有didStartMonitoringRegion。有沒有什麼方法我不小心使用Mac庫而不是iOS庫?

回答

0

您所在地區的半徑太小,因此您無法獲得準確的取件。嘗試將它從半米擴展到10或20等,並從那裏開始工作。

你也可以嘗試設置精度,但半徑是你的主要問題,我幾乎可以肯定。

更新

我剛纔注意到您呼叫-startUpdatingLocatuon而不是-startMonitoringRegion。嘗試在構建CLRegion的地方添加代碼,並將其添加到監控中。

+0

對不起,我意識到,固定它,我張貼後。它沒有幫助。 – jab 2012-02-20 22:06:40

+0

無論如何,我問是不是檢測區域進入/退出的問題,這就是爲什麼'didStartMonitoringForRegion'委託方法不會被調用(從來沒有了,但我發誓我看到了它的工作一次或兩次)。 – jab 2012-02-20 22:10:10

+0

重新您的更新:我已經做了好辦法,但在此實現,'startMonitoringRegion'被稱爲對象初始化後立即(在'init'方法完成後右)。在那裏添加延遲沒有幫助。 – jab 2012-02-21 18:45:35

3

見蘋果說什麼:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

的應用測試的地區監視支持

當iPhone模擬器或設備上測試你的區域監管碼,實現該地區的活動可能不會在跨越區域邊界後立即發生。爲了防止虛假通知,iOS在達到某些閾值條件之前不會傳遞區域通知。具體而言,用戶的位置必須穿過區域邊界並離開該邊界最小距離,並且在報告通知之前保持該最小距離至少20秒。

特定閾值距離由硬件和定位技術當前可用來確定。例如,如果Wi-Fi已禁用,則區域監控的精確度會大大降低。但是,出於測試目的,您可以假定最小距離大約爲200米。

+0

這是一個好消息,但這不是問題。該'didEnterRegion'和'didExitRegion'委託方法稱爲(最終,像你說的)。但是'didStartMonitoringForRegion'不會被調用,即使外景經理清楚地監視區。這裏 – jab 2013-04-11 19:20:02

+0

同樣的問題,如上面所描述的評論,並ü找到紐約的解決方案? – nidIOS 2014-05-30 14:09:15