【问题标题】:Function "didUpdateToLocation" being called without changes调用函数“didUpdateToLocation”而不进行更改
【发布时间】:2012-09-28 04:03:55
【问题描述】:

我这样初始化 locationManager:

if (!self.locManager) 
{
    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    [locManager startMonitoringSignificantLocationChanges];
}

我的设备没有移动,并且每次都在调用“didUpdateToLocation”。 可能是什么问题? 谢谢

【问题讨论】:

    标签: iphone ios location mkmapview cllocationmanager


    【解决方案1】:

    didUpdateToLocation 可能会因多种原因而更新,处理此问题的一个好策略是根据时间戳逐步过滤结果,然后根据要求的准确性。

    苹果在LocateMe sample app中提供了一个很好的例子:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        // 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 (self.bestEffortAtLocation == nil || self.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")];
            }
        }
    }
    

    【讨论】:

    • 谢谢,但如果我希望定位服务保持打开状态怎么办? stopUpdating 可能会停止位置管理器吗?
    • 此代码允许位置管理器根据需要经常更新,以将用户定位到所需的准确性。此时,电源管理停止更新是礼貌和明智的。如果您的应用需要在用户更改位置时对其进行监控,那么您可以注册重大更改:[locationManager startMonitoringSignificantLocationChanges]; 请参阅docs
    【解决方案2】:

    您是否检查位置差异? CoreLocation 也会在精度、航向或速度等其他属性发生变化时调用回调

    startMonitoringSignificantLocationChanges应该给你一个初始修复,然后你会收到“重大变化”的回调(蜂窝塔变化等)

    【讨论】:

    • 对不起,我没有完全理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2022-01-22
    • 2021-11-26
    • 1970-01-01
    • 2011-11-01
    相关资源
    最近更新 更多