为什么didUpdateLocation没有运行但我的GPS坐标仍在更新?

时间:2012-08-10 09:22:08

标签: ios xcode core-location

我正在尝试构建一个应用程序,该应用程序以固定的时间间隔提供GPS坐标以显示在标签上并发送到Web服务器,但这证明很难。我已经设法让它给我准确的实时GPS坐标,但我必须按一个按钮才能刷新标签。

我认为didUpdateLocation方法现在是一个好的地方,但似乎永远不会运行。我正在通过包含一个NSLog帖子对此进行测试 - 当我使用Apple的LocateMe示例应用程序执行此操作时,它会像应该的那样运行。

以下是我的方法:

- (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation
{
     theLocation = [NSString stringWithFormat:@"latitude: %f
        longitude: %f", locationManager.location.coordinate.latitude,
        locationManager.location.coordinate.longitude];

     NSLog(@"location manager did something!!!");
}

虽然我猜测问题出在上述方法之外。我想我应该将viewDidLoad方法包含在startUpdatingLocation的位置:

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [outletLabelData setText:(@"Loaded")];
}

我也尝试在我的按钮操作方法中使用startUpdatingLocation,甚至让它像启动和停止更新的开关一样工作(应该触发更新事件),但仍然没有成功。

其他细节:

  • Xcode 4.4
  • iOS 5.1
  • 在我的 iPhone上插入我的Mac
  • 时测试应用

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您需要告诉您CCLocationManager谁是代表。在你的情况下,它是你当前的控制器。

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];

并且显然确保您的控制器符合CLLocationManagerDelegate协议。

相关问题