2011-03-30 48 views
3

我想问一下关于停止CLLocationManager -startUpdatingLocation的建议。目前,我正在考虑两种方法,但我不能确定使用哪一个,并希望得到一个想法其他人是如何做到这一点:停止CLLocationManager?

Method_001:

[locationManager startUpdatingLocation]; 
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"TimedOut" afterDelay:30]; 
  • 潜在浪费电池寿命一如既往运行30秒
  • 如果网络速度较慢可能无法及时获得准确位置
  • 感觉实现超时的方法非常简单。

Method_002:

[locationManager startUpdatingLocation]; 

然后内:-locationManager:didUpdateToLocation:fromLocation:添加:

static int timeOut = 0; 
timeOut++; 

// Other code that checks for and stops 
// when a suitable result, accuracy, age etc is found. 

if(timeOut >= 4) { 
    [[self locationManager] stopUpdatingLocation]; 
    timeOut = 0; 
    return; 
} 
  • 可能无法在4(或更小)的尝试解决准确位置。
  • 对于CLLocationManager,可能不会返回4个结果,我们从不超时。
  • 更好的电池寿命,因为我们立即停止一个好的结果。

只是好奇吗?

回答

2

嗯,我我认为我更喜欢第一个。我不知道我们是否可以确定didUdpateToLocation:方法被调用的频率。我认为时间更加可靠。

+0

这就是我在想什么,目前我正在使用method_002,但注意到有时没有第四次调用委托。这似乎发生在第一次回报特别准确的情况下。同样,如果将3设置为超时,则特别困难的情况(小区覆盖不良)不能准确解析到可用位置。 – fuzzygoat 2011-03-30 18:48:18

3

不确定你想要做什么,但我认为CLLocationManager内部处理这些情况。只是配置它这样的:

locManager.desiredAccuracy = 2000.0f; // 2 kilometers - hope for accuracy within 2 km. 
locManager.distanceFilter = 1000.0f; // one kilometer - move this far to get another update 

,然后在回调didUpdateToLocation:fromLocation: 如果你有一个积极signbit,

[locManager stopUpdatingLocation]; // stop GPS 

编辑:添加signbit

if (signbit(newLocation.horizontalAccuracy)) { 
     // Negative accuracy means an invalid or unavailable measurement, so punt. 
} else { 
     // this is a usable measurement. 
    } 
+0

请问CLLocationManager到选择 '禁用' 回应? – atreat 2012-08-17 19:06:07

+0

@atreat - 良好的捕获 - 这是我的代码中的自定义包装方法。我已更新为使用正确的方法调用。 – Rayfleck 2012-08-21 18:03:41

+0

你是什么意思“如果你有一个积极的signbit,” – 2014-01-27 15:50:21

1

为什么不给这两种方法结合起来,并给第三

我写了一篇关于这个GIT回购,你是免费的(我最好的结果并没有在一定的时间提高)使用 https://github.com/xelvenone/M6GPSLocationManager

  • 如果结果精度高于acceptableAccuracy更好,我们完成
  • 如果我们得到occuracy的更新,我们等待maximumWaitTimeForBetterResult以获得更好的一个, - 如果不这样做,我们正在这样做,并采取最好的一个
  • 如果我们不断得到更新,超过最大尝试次数,我们会采取最好的一次(可能我们正在移动)
  • 如果我们在30秒内没有得到任何其他更新,我们就完成了(不会有可能任何其他更新)

代码

- (void)scopeToCurrentLocationWithAcceptableAccuracy:(CLLocationAccuracy)acceptableAccuracy 
        maximumWaitTimeForBetterResult:(NSTimeInterval)maximumWaitTimeForBetterResult 
           maximumAttempts:(NSInteger)maximumAttempts 
            onCompletion:(M6GPSLocationManagerCompletion)completion;