【问题标题】:didUpdateToLocation doesn't gets called immediatelydidUpdateToLocation 不会立即被调用
【发布时间】:2011-11-01 16:25:00
【问题描述】:

我正在使用

startMonitoringSignificantLocationChanges

用于获取 (lat,lon) 值。我的问题是

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在遇到[locationMangerObject startMonitoringSignificantLocationChanges]; 后不会立即调用该方法。在didUpdateToLocation 方法中,我只维护两个全局双变量,用于保存从didUpdateToLocation 方法获得的坐标设置的纬度和经度。这些值被传递给 web 服务,因为 didUpdateToLocation 只被调用了一定的延迟,在这之间,(lat,lon) 的参数取零作为它们的值并传递给服务,这会导致意外的响应。

制作方法

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在 `[locationMangerObject startMonitoringSignificantLocationChanges]; 之后立即调用的方法遇到了。

请任何人建议我解决此问题的解决方案。

提前致谢。

【问题讨论】:

  • 设置 locationMangerObject.delegate = self;

标签: iphone cllocationmanager


【解决方案1】:

可能需要几秒钟到几分钟的时间才能找到一个好的位置,具体取决于您是否有手机或 wifi 连接,以及您是否有来自 GPS 卫星的良好信号。但是,您应该立即获得一个缓存的(可能是过时的)位置。

您是否为您的 locationMangerObject 设置了委托?

您的函数在调用 startMonitoringSignificantLocationChanges 后是否返回到 runloop?

【讨论】:

  • 我已经设置了委托。控制到[locationManagerObject startMonitoringSignificantLocationChanges]后;它不执行 didUpdateToLocation。执行被定向到 viewDidLoad 中的其他语句,这些语句在 locationManager 初始化和其他位置管理器的东西之后出现,我正在 viewDidLoad 中初始化位置管理器
  • 我在控制台中收到此消息,是否与位置管理器“注册计时器已过期,但客户端仍在注册!”有关!
  • 你能多展示一点你的代码,你开始监控的地方和你期望回调的时间吗?
【解决方案2】:

也许您应该考虑使用 startUpdatingLocation 方法而不是 startMonitoringSignificantLocationChanges。来自 iOS 文档:

startUpdatingLocation

此方法立即返回。调用此方法会使位置管理器获取初始位置修复(可能需要几秒钟)并通过调用其 locationManager:didUpdateToLocation:fromLocation: 方法通知您的委托。

【讨论】:

    【解决方案3】:
    ///Hmmmmm
    
                ...CLLocationManager does n't call delegate method properly 
                ...after lot of R&D I'm  using a watchdog method it calls " -(void)locationadd " function every 10seconds and i'm using that location for drawing the path.
    
      -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
         {
         }       
    
    
    
     - (void)viewDidLoad
         {
    
       [NSTimer scheduledTimerWithTimeInterval:10
                                             target:self
                                               selector:@selector(locationadd)
                                               userInfo:nil
                                               repeats:YES];
    
            }
    
    
            -(void)locationadd   
            {
    
             if ([locationManager location]) {
    
    
    
            //i'm saving this location
    
        CLLocation *locat=locationManager.location;
    
         }
    
            }
            //
    

    【讨论】:

      【解决方案4】:

      这是因为方法startMonitoringSignificantLocationChanges() 监控位置的显着变化。因此,当过去位置更新的位置或时间戳存在测量差异时,它不会一直更新位置。

      最好使用startUpdatingLocation(),它会被频繁调用,因此您将在didUpdateLocations() 中获得位置变化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        相关资源
        最近更新 更多