获取位置坐标时间的问题

时间:2011-06-28 14:53:29

标签: iphone core-location cllocationmanager

在我的应用上设置位置坐标时会出现问题。

我一直在测试我的应用程序的位置坐标来自模拟器和坐在我家的iphone(它不仅仅是我的家,我在不同的位置(室外)测试它以及非常好的网络连接),以及我看到这种有线行为,我现在有正确的坐标,然后我将应用程序发送到后台并将其带回来我得到正确的位置坐标,如果我做了8-10次(即发送到应用程序来自背景之后不久,我无法获得位置坐标,此时获得位置坐标的唯一方法是杀死应用程序然后重新开始。所以我确定有些事情出了问题,但我不确定它是什么。

这就是我正在做的事情

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
   //if the time interval returned from core location is more than 30 seconds we ignore it because it might be from an old session
   if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 30) {

        if(newLocation.coordinate.latitude != previousLocation.coordinate.latitude && newLocation.coordinate.longitude != previousLocation.coordinate.longitude){

        if(newLocation.horizontalAccuracy <= 100){
            [self.locationManager stopUpdatingLocation];
            [self.locationManager startMonitoringSignificantLocationChanges]; 
   }
   else{
        [self.locationManager startUpdatingLocation];
   }

所以基本上我只接受newLocation,如果它不是30秒,并且它与我在本地存储的前一个位置不同,水平精度小于100米。当我在调试器中运行它我正在观察的是我到达第一个if条件3-4次并且如果它失败它不会在那之后,这意味着didUpdateToLocation根本不被调用。

一旦坐标符合我的所有标准,我就会停止更新位置并执行startMonitoringSignificantLocationChanges。

我在我的else块中执行startUpdatingLocation的原因是。

例如,如果由于startMonitoringSignificantLocationChanges而调用didUpdateToLocation,我希望在此之后得到准确的位置,所以每次我没有找到正在寻找的正确位置时我正在执行startUpdatingLocation,因为我相信做多个startUpdatingLocation没有什么都不会伤害。

如果我的思考过程或代码逻辑中存在错误,请告诉我。

1 个答案:

答案 0 :(得分:0)

我要检查的第一件事是你的info.plist。您必须包含密钥所需背景模式 - &gt;应用程序注册位置更新。如果没有这个,您将无法在后台收到位置更新(除了重要的位置更改和区域监控)。

在main else中,您调用startUpdatingLocation。虽然我不相信这会伤害任何东西,除非你用stopUpdatingLocation来平衡它,我不相信它会做任何事情。文档说:

  

连续多次调用此方法不会自动导致生成新事件。但是,在两者之间调用stopUpdatingLocation会导致在下次调用此方法时发送新的初始事件

但无论如何,我认为你不需要告诉它再次启动更新,当超出distanceFilter属性或硬件收集更准确的位置读数时,它将继续自行启动。

我没有看到你的第二个关闭}。