开发者

iOS: Core location turn off after update?

Obviously to save battery we need to use CoreLocation as quickly as possible and shut it off when not needed. I have a GPS app that tracks a users location, so I use almost all good location updates. Do I still need to be turing off core location at some sort of interval?

Looking in Apple's "LocateMe" app they seem to turn it off when they find a location, but this is confusing to me, when does it get turned back on? In my case it doesn't seem to make sense to turn it off for a split second then back on.

Thoughts?

From "LocateMe":

- (void)locat开发者_运维百科ionManager:(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];    
}


It seems to me that your dual requirements of wanting location updates every meter or so vs. wanting to save battery and so wanting to use CoreLocation as quickly as possible and then shut it off "when not needed" are mutually incompatible. If you need location updates every meter or so, there is no time when the app will be in the foreground when you will not need location services on. Depending on the nature of your app, you may well want to turn location services off when you go into the background or the screen is locked...

The one other exception might be that you'd like to turn off location services if the user is standing still for a while. Apple provide the significant change location service but that isn't going to be suitable for your needs it would seem, if you want meter-by-meter updates... it just doesn't have that level of resolution.

So there may be no solution that meets both your desires?


it depends on how often you need location. If you need it all the time while your app is working its a better a idea to turn ON location updates in applicationWillBecomeActive and turn OFF the updates in applicationWillResignActive


You can configure the accuracy and distance filter

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

it depends on accuracy and update frequency you need

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜