2011-12-21 56 views
6

我想使用監控區域來跟蹤用戶是否曾訪問過地標。 外景經理是在視圖控制器初始化在視圖控制器的viewDidLoad中一個mapkitiphone CLLocationmanager區域監控回調未觸發

沿:

if (self.locationManager == nil) 
{ 
    //  NSLog(@"creating location manager"); 
    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
} 


NSSet* set=[locationManager monitoredRegions]; 

if ([CLLocationManager regionMonitoringAvailable] && [CLLocationManager regionMonitoringEnabled]) { 
    NSLog(@"region monitoring okay"); 
    NSLog(@"monitored regions: %@",set); 
} 

我得到的NSLogs「區域監控好」正確和所有地區。

加入地區都像這樣

double metres=20.0; 
CLLocationDistance dist=metres; 
CLLocationAccuracy acc=1.0; 

CLRegion *reg=[[CLRegion alloc] initCircularRegionWithCenter:coordinate radius:dist identifier:landmarkObj.landmarkName]; 

[locationManager startMonitoringForRegion:reg desiredAccuracy:acc]; 

完成,但回調都不會觸發

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Entered" 
                message:region.identifier 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil, nil]; 
    [alert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exited" 
                message:region.identifier 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil, nil]; 
    [alert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region 
{ 
    NSLog(@"started monitring for region: %@",region); 
} 

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

然而更新的位置,工作正常。

[locationManager startUpdatingLocation]; 

觸發回調didUpdateToLocation預期

更新:使用didUpdatToLocation監視的區域來代替。 仍然有興趣知道爲什麼這不起作用,但看起來很少有成功的區域監測

+0

您是否找出瞭解決此問題的解決方案?我遇到了同樣的問題,並且StartStonMonitoringForRegion沒有啓動。 – Das 2012-08-06 03:15:58

+0

事實證明,區域監控並不能提供我需要的精度。我最終爲每個didUpdateToLocation回調進行手動檢查。這對我來說是可行的,因爲我只跟蹤10個區域 – tzl 2012-08-24 06:47:40

回答

3

區域跟蹤的內容是針對低粒度的位置跟蹤,並在單元格位置邊界上觸發,因此如果您不跨越單元邊界,則永遠不會檢查您的區域。我有同樣的問題,並研究這一點,並在不同的網站有關於這個評論其指出,這個蘋果論壇:

https://devforums.apple.com/message/251046#251046

如果你讀你就會明白,爲什麼這是行不通的意見。

我正在嘗試解決我在哪裏定義我自己的NSSets來包含跟蹤的CLRegions和佔用CLRegions,然後當我得到一個locationManager:didUpdateToLocation:fromLocation:回調,我檢查我的跟蹤集中的所有區域,看看我是否不在inRegions集中,但現在在追蹤區域(添加inRegions並用enterRegion回調),或者如果我是inRegion但現在不是(從inRegions中移除並使用exitRegion回調)。這是一項正在進行的工作。

+0

不,事實並非如此,因爲我在同一個單元載體上有兩個不同的設備,其中的區域在一個區域上工作,但是沒有其他 – uofc 2014-09-10 19:41:15

+0

好,是的,情況就是這樣。這可能不是完全的情況,因爲可能涉及其他問題,但它圍繞着細胞塔的覆蓋範圍以及改變細胞和類似的東西來觸發。由於GPS價格昂貴,通常不使用GPS。它可能有很多細微差別,邊緣情況和其他東西,如WiFi和涉及的東西。閱讀上面的Apple鏈接,以更好地瞭解問題。 – chadbag 2014-09-11 20:02:47

0

你是否設置CLLocationManagerDelegate到您的ViewController?

@interface Maps : UIViewController <CLLocationManagerDelegate>{ 
... 
} 
+0

是的,我做了\t @interface TrailViewController:UIViewController tzl 2011-12-21 12:53:54

+2

這實際上是不相關的。不在協議中聲明隻影響編譯器的警告和東西,但不影響實際的委託調用回你的委託。 – chadbag 2012-02-04 22:22:39

0

嘗試使用調試 - >位置 - >高速公路驅動器模擬,我有一個類似的問題,但我創建了一些地區和didEnterRegion被觸發以超過1000米... ...我不不知道方式..我已經設置:

locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
0

你對你的中心座標有什麼作用?你的例子中缺少了這部分。我看過其他的例子,他們沒有用足夠精確的座標來結合區域的半徑來觸發它。如果要在20米半徑處觸發,必須輸入相當精確的座標(可能爲5或6位小數)。

其他一切看起來都不錯。

+0

我使用6位小數。最令我感到困惑的是,didStartMonitoringForRegion沒有被觸發:S – tzl 2011-12-29 03:42:56

+0

我會嘗試更大的半徑來啓動,看看是否有幫助。在我將所有內容都移入我的AppDelegate之後,我只能夠讓區域工作。然後我得到了我需要的所有回調。 – 2011-12-29 13:39:06

0

您還需要NSLocationAlwaysUsageDescription在你的plist中,並需要調用[的LocationManager requestAlwayAuthorization]在您的應用程序。 NSLocationWhenInUseUsageDescription將關閉區域監視。