2012-06-05 23 views
2

我正在構建一個專門針對基於位置的應用程序的應用程序,因此我需要獲取用戶座標經度和緯度及其工作。CoreLocation region delegates not called

現在我需要知道用戶是否使用CLLocationManager代表輸入了預定義的某個區域,因此我使用NSLog語句覆蓋didEnterRegion以瞭解用戶是否輸入區域。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

我叫startMonitoringForRegion:但像didEnterRegionmonitoringDidFailForRegion代表或爲任何CLRegion代表沒有發生。

下面是我的一些代碼:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
     locLongittude=newLocation.coordinate.longitude; 
     locLattiude=newLocation.coordinate.latitude; 

     self.regionTimer=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(monitorRegion) userInfo:nil repeats:NO]; 
    } 

    -(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius 
    { 
     CLLocationCoordinate2D home; 
     home.latitude = latitude; 
     home.longitude = longitude; 

     CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:@"home"]; 

     if([CLLocationManager regionMonitoringEnabled] && [CLLocationManager regionMonitoringAvailable]) 
      [locManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest]; 

     NSLog(@"Count: %d region aval %d enable %d",[[locManager monitoredRegions]count], [CLLocationManager regionMonitoringAvailable],[CLLocationManager regionMonitoringEnabled]); 
     for(CLRegion* region in locManager.monitoredRegions) 
     NSLog(@"%@", region); 
    } 

    -(void)monitorRegion 
    { 
     [self startMonitor:31.971160 longitude:35.832465 radius:10000.0]; 
    } 

編輯:使用模擬器進行模擬和使用產品 - >調試的位置,所有的測試 - >模擬位置

玉傢伙它的工作原理。上面的代碼很好用,問題在於使didEnterRegion方法被調用的方法是在您需要輸入的區域之外,然後輸入它以使其工作。

我在做的是在模擬中,我總是選擇一個座標,在我需要輸入的區域內,這是它必須在區域外的問題。

因此,這裏是我的代碼與您分享

更新:我很奇怪,爲什麼當我離開該地區didExitRegion不會被調用?

+0

你能打印出地區,所以你知道他們被添加到監視器堆棧? – Jake

+0

是它添加(現在我只有一個區域) 2012-06-05 12:30:15.929 GPS_Location [1415:f803](identifier home)<+ 31.97116089,+ 35.83246613> radius 10000.00m –

回答

0

根據我自己的經驗,它看起來像區域監視取決於細胞變化事件(蘋果爲什麼這樣設計它?我想延長電池壽命)。所以這個實現並不理想。

理想情況下,只要你跨越邊界,它應該工作,但它不會以同樣的方式在iPhone上發生。即使使用內置的提醒應用程序,有時也會因爲附近的位置提醒而失敗。

所以在你的情況下,我認爲單元格更改事件不會發生,並且爲什麼didExitRegion不被調用。您可以使用startMonitoringSignificantLocationChanges來仔細檢查它。