2013-05-20 49 views
2

我试图为LBS应用程序进行实验时遇到了问题。当在模拟器中测试时,它似乎工作。然后,我在iPhone上试了一下。它没有按预期工作。CLLocationManager没有正确更新数据

当我启动应用程序时,它会显示所有信息,例如我的位置纬度,经度,距离等等。但是,当我走开时​​,这些信息没有改变。无论是运行应用程序还是我的iPhone自动关闭(然后再次打开应用程序),这些数据仍然保持不变。

虽然我关闭应用程序并重新启动它,但它可以显示更新信息。

这里是我的代码

我成立了CLLocationManager在viewDidLoad中

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //setup locationManager 
    locationManager =[[CLLocationManager alloc]init]; 
    locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
    locationManager.distanceFilter=kCLDistanceFilterNone; 
    [locationManager setDelegate:self]; 
    [locationManager startUpdatingLocation]; 

} 

这里是CLLocationManager代表

码 - (空)的LocationManager:(CLLocationManager *)经理didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

if (newLocation.horizontalAccuracy < 0) return; 

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

if (locationAge > 5.0) return; 


if (myLocation == nil || myLocation.horizontalAccuracy > newLocation.horizontalAccuracy) { 

    self.myLocation = newLocation; 
    // NSLog(@"mylocation=%@",self.myLocation); 
    [self.mainTableView reloadData]; 
    [self sortedDistArray];//this method is to calculate distance and put it in array. It work... 


    if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) { 
     [self stopLocationManager];  
    } 
} 

}

  • (无效)stopLocationManager { [的LocationManager stopUpdatingLocation]; locationManager.delegate = nil; }

现在,我有以下问题。

  1. 我的CLLocationManager设置有什么问题?我应该在appDelegate下面(在didFinishLaunchingWithOptions方法下)让它运行它的背景吗?它会快速耗尽电池电量吗?或者将它设置在ViewWillAppear而不是ViewDidLoad中?

  2. 另一方面,我把'拉下来刷新'到程序中。我认为刷新它时可以更新更新的数据。但是,它不会起作用。这里是我的刷新代码(我把EGOTableViewPullRefresh用于刷新功能)。

    • (无效){reloadTableViewDataSource

      // should be calling your tableviews data source model to reload 
          // put here just for demo 
      
      _reloading = YES; 
      
      locationManager =[[CLLocationManager alloc]init]; 
      locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
      locationManager.distanceFilter=kCLDistanceFilterNone; 
      [locationManager setDelegate:self]; 
      [locationManager startUpdatingLocation]; 
      
      [self sortedDistArray]; 
      [mainTableView reloadData]; 
      
      [self doneLoadingTableViewData]; 
      

      }

什么是我的想法是CLLocationManager复位并开始更新位置时用户刷新。但是,它没有工作。我的错是什么?

对不起,我有这么多问题。如果有人给我建议,我将不胜感激。

+0

检查文档怎么我认为这是对的位置两次连续调用之间的时间延迟。 –

回答

0

您无法控制何时提供位置更新,您只能启动CLLocationManager,操作系统会决定何时为您的CLLocationManagerDelegate提供更新。

如果您希望它强制更新,您可以尝试检查CLLocationManager是否已经启动,如果是,请停止并重新启动。顺便说一下,作为一个附注,我建议你将CLLocationManager包装在一个单例类中。如果你继续启动位置管理器,它会在很多情况下使用应用程序运行并耗尽电池(当你实际停止时,我看不到你的代码)。根据需要启动和停止CLLocationManager

0

我不清楚你到底想要什么。如果您只想在打开应用程序时显示位置,请将startupdatinglocation行移至viewDidAppear。在屏幕上显示一个活动指示器,因为它需要一些时间回到并更新。

如果您希望它不断更新,请不要调用stopUpdatingLocation。只要您的位置更改以符合经理的设置,它就会触发委托方法。

另一件事我注意到,你的意思是说

if (locationAge < 5.0) return; 

看来你比5.0大说在这种情况下,如果花费长于得到更新,你将永远不会到其他回应。

而不是重复设置您的经理,创建一个单身或保存在一个属性。一旦启动,您只需根据需要启动/停止更新。

您可能会受益于一些我在这里的技巧:

TTLocationHandler GitHub Repository

+0

由于笔记本电脑坏掉了T_T,我刚回到这里。感谢您的慷慨建议。事实上,我的问题是在某些地下区域不能工作。 TTLocationHandler是否在地下工作?我只想让用户可以在地下触发应用程序(即我不会尝试通过地下区域的转向导航)。 –

+0

还有一件事...我找到了关于将CLLocationManager包装在Internet上的单例类中的方法。链接是http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/....希望它能帮助像我这样的人 –