2012-07-14 80 views
4

我有一個應用程序,它顯示用戶位置的地圖並跟蹤用戶,直到應用程序移動到後臺。此時,位置管理器被告知停止更新位置,而是監視一個區域(最近已知位置周圍的100米半徑)。在iOS模擬器上,它按預期工作,並顯示Geofencing指標(與常規位置服務指標相同,但僅限大綱)。在iPhone上,它似乎按預期工作,但顯示常規位置服務圖標而不是單獨的輪廓。地理柵欄指示與定位服務指標

有沒有什麼理由可能會發生這種模擬器和手機之間的差異?我只想確保手機確實只使用Geofencing,並且沒有其他服務(即確保電池使用率最低)。

其他信息:

我都需要後臺模式設置爲接收位置更新 - 這是在特定情況下(當用戶啓用它),而不是所有的時間。我試過禁用這個,但是,問題依然存在。

我使用利於讓一個後臺應用程序的代碼:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Only monitor significant changes – unless specified 
    if(!self.viewController.userSpecifiedToUseLocationServices) 
    { 
     // Stop using full location services 
     [self.viewController.locationManager stopUpdatingLocation]; 

     // Create a boundary -- a circle around the current location 
     self.viewController.currentBoundary = [[CLRegion alloc] initCircularRegionWithCenter:self.viewController.locationManager.location.coordinate radius:kSignificantLocationChange identifier:@"Current Location Boundary"]; 

     // Monitor the boundary 
     [self.viewController.locationManager startMonitoringForRegion:self.viewController.currentBoundary desiredAccuracy:kCLLocationAccuracyBest]; 
    } 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Stop monitoring the region 
    if(self.viewController.currentBoundary != nil) 
    { 
     [self.viewController.locationManager stopMonitoringForRegion:self.viewController.currentBoundary]; 
     self.viewController.currentBoundary = nil; 
    } 

    // Start full location services again 
    [self.viewController.locationManager startUpdatingLocation]; 
} 

回答

5

嗯,首先,在仿真器上定位服務從來沒有真正的設備的高保真表示。所以,我不一定認爲你會在兩個測試平臺上看到相同的東西。

其次,根據this answer on stack overflow,這聽起來像是iOS 5的正常行爲(顯示不同的指標)。

我還要警告地理圍欄不是一項神奇的技術。該設備仍然必須使用位置服務,這將耗盡電池。我肯定會推薦使用Apple的startMonitoringForRegion:startMonitoringForRegion:desiredAccuracy:並利用它們的實現,而不是編碼自己的。但是,我也不希望電池耗盡可以忽略不計。

最後,您指定的精度爲kCLLocationAccuracyBest,而不是使用startMonitoringForRegion:,或指定較低的精度要求。我不明白不是會如何影響電池性能。更高的準確性意味着操作系統將不得不得到更高質量的修補程序,或者更頻繁地進行輪詢,或者兩者兼而有之。

可悲的是,世上沒有免費的午餐:(

+1

優秀!感謝您的鏈接。事實證明,我已經開始監測在以前版本的應用程序的顯著位置變化,它只是從來沒有停止過。 – 2012-07-16 20:16:23