4

我有一个应用程序,它显示用户位置的地图并跟踪用户,直到应用程序移到后台。此时,位置管理器被告知停止更新位置,而是监视一个区域(最后一个已知位置周围 100 米半径)。在 iOS 模拟器上,它按预期工作并显示地理围栏指示器(与常规位置服务指示器相同,但只有轮廓)。在 iPhone 上,它似乎按预期工作,但显示常规位置服务图标而不是单独的轮廓。

是否有任何原因可能会发生这种情况,模拟器和手机之间的这种差异? 我只是想确保手机真的只使用地理围栏而没有其他服务(即确保最小的电池使用量)。

附加信息:

我需要设置后台模式来接收位置更新——这是在特定情况下(当用户启用它时)而不是一直。但是,我尝试禁用此功能,但问题仍然存在。

我用于后台应用程序的代码:

- (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];
}
4

1 回答 1

5

嗯,首先,模拟器上的定位服务从来都不是真实设备的高保真表示。因此,我不一定假设您会在两个测试平台上看到相同的内容。

其次,根据这个关于 stack overflow 的答案,这听起来像是 iOS 5 上的正常行为(显示不同的指标)。

我还要警告说,地理围栏不是一种神奇的技术。该设备仍然必须使用定位服务,这会耗尽电池电量。我当然会推荐使用 Apple 的startMonitoringForRegion:orstartMonitoringForRegion:desiredAccuracy:并利用他们的实现,而不是自己编写代码。但是,我也不希望电池消耗可以忽略不计。

最后,您将精度指定为kCLLocationAccuracyBest,而不是使用 startMonitoringForRegion:,或指定较低的精度要求。我不明白这不会影响电池性能。更高的准确性意味着操作系统要么必须获得更高质量的修复,要么更定期地轮询,或两者兼而有之。

可悲的是,没有免费的午餐:(

于 2012-07-15T07:07:36.227 回答