2

当我尝试尝试 LBS 应用程序时,我遇到了问题。在模拟器中测试时,它似乎工作。然后,我在 iPhone 上试了一下。它没有按预期工作。

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

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

这是我的代码

我在 viewDidLoad 中设置了 CLLocationManager

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

}

这是 CLLocationManager 代表的代码

-(void)locationManager:(CLLocationManager *)manager 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];    
    }
}

}

  • (void)stopLocationManager{ [locationManager stopUpdatingLocation]; locationManager.delegate = nil; }

现在,我有以下问题。

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

  2. 另一方面,我将“下拉刷新”放入程序中。我认为可以在刷新期间更新更新的数据。但是,它也不会起作用。这是我的刷新代码(我将 EGOTableViewPullRefresh 用于刷新功能)。

    • (void)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 在用户刷新期间重置并开始更新位置。但是,它没有用。我的错是什么?

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

4

2 回答 2

0

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

如果您希望它不断更新,请不要调用 stopUpdatingLocation。每当您的位置更改以满足经理的设置时,它将触发委托方法。

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

if (locationAge < 5.0) return;

似乎您说比 5.0 更好,在这种情况下,如果获得更新的时间比这更长,您将永远不会回复另一个。

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

您可能会从我的一些提示中受益:

TTLLocationHandler GitHub 存储库

于 2013-05-21T01:21:18.093 回答
0

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

如果您希望它强制更新,您可以尝试检查是否CLLocationManager已启动,如果已启动,请停止并重新启动。

顺便说一句,我建议您将 CLLocationManager 包装在单例类中。如果您继续启动位置管理器,它将在许多情况下与应用程序一起运行并耗尽电池(当您实际停止它时,我在您的代码中看不到)。根据需要启动和停止CLLocationManager

于 2013-05-20T14:51:06.383 回答