iOS:核心位置在更新后关闭?

时间:2011-09-27 05:13:29

标签: iphone ios gps core-location cllocationmanager

显然,为了省电,我们需要尽快使用CoreLocation,并在不需要时将其关闭。我有一个跟踪用户位置的GPS应用程序,因此我几乎使用了所有良好的位置更新。我是否还需要在某种间隔内找到核心位置?

在Apple的“LocateMe”应用程序中,他们似乎在找到位置时将其关闭,但这对我来说很困惑,何时会重新开启?在我的情况下,将它关闭一瞬间再重新开启似乎没有意义。

思想?

来自“LocateMe”:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // store all of the measurements, just so we can see what kind of data we might receive
    [locationMeasurements addObject:newLocation];
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;
    // test the measurement to see if it is more accurate than the previous measurement
    if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
        // store the location as the "best effort"
        self.bestEffortAtLocation = newLocation;
        // test the measurement to see if it meets the desired accuracy
        //
        // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
        // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
        // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
        //
        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
            // we have a measurement that meets our requirements, so we can stop updating the location
            // 
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            //
            [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];
            // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];
        }
    }
    // update the display with the new location data
    [self.tableView reloadData];    
}

3 个答案:

答案 0 :(得分:1)

在我看来,你想要每隔一米左右更新位置的双重要求与想要节省电池,以便尽可能快地使用CoreLocation然后关闭“不需要时”互不相容。如果您需要每米左右进行一次位置更新,那么当您不需要位置服务时,应用程序就没有时间位于前台。根据应用程序的性质,您可能希望在进入后台或屏幕被锁定时关闭位置服务...

另一个例外可能是,如果用户静止一段时间,您想要关闭位置服务。 Apple提供the significant change location service,但如果您想要逐米更新,那么这似乎不适合您的需求......它只是没有那么高的分辨率。

所以可能没有满足你的愿望的解决方案?

答案 1 :(得分:0)

这取决于您需要定位的频率。如果您在应用程序工作时一直需要它,那么最好在applicationWillBecomeActive中打开位置更新并关闭applicationWillResignActive中的更新

答案 2 :(得分:0)

您可以配置精度和距离过滤器

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10;

这取决于您需要的准确性和更新频率