简体   繁体   English

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

[英]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. 显然,为了节省电池,我们需要尽快使用CoreLocation,并在不需要时将其关闭。 I have a GPS app that tracks a users location, so I use almost all good location updates. 我有一个GPS应用程序可跟踪用户的位置,因此我几乎使用了所有良好的位置更新。 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? 在Apple的“ LocateMe”应用程序中查找时,他们似乎在找到位置时将其关闭,但这使我感到困惑,何时将其重新打开? In my case it doesn't seem to make sense to turn it off for a split second then back on. 就我而言,将其关闭一秒钟然后重新打开似乎没有任何意义。

Thoughts? 有什么想法吗?

From "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];    
}

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. 在我看来,您的双重要求是:相互之间不兼容,即需要位置每米更新一次,而要节省电池电量,因此要尽快使用CoreLocation,然后在“不需要时将其关闭 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. Apple提供了重要的更改位置服务,但如果您想要逐米更新,它似乎并不适合您的需求……它只是没有这种分辨率。

So there may be no solution that meets both your desires? 因此,可能没有满足您两个愿望的解决方案吗?

You can configure the accuracy and distance filter 您可以配置精度和距离过滤器

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

it depends on accuracy and update frequency you need 这取决于您需要的准确性和更新频率

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 如果您在应用程序运行过程中始终需要它,那么最好在applicationWillBecomeActive中打开位置更新并在applicationWillResignActive中关闭更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM