核心位置无法在viewDidLoad中获取数据

我写了一个简单的iOS应用程序,检索位置信息,然后使用该位置来请求雅虎天气。

问题是,即使我调用viewDidLoad中的核心位置,它不会立即给我的结果。

那为什么我不能得到位置信息?

如何获取viewDidLoad中的位置信息?

伪代码目前是这样的:

- (void)viewDidLoad { [super viewDidLoad]; self.locManager = [[CLLocationManager alloc] init]; self.locManager.delegate = self; self.locManager.desiredAccuracy = kCLLocationAccuracyBest; self.locManager.distanceFilter = 100; [self.locManager startUpdatingLocation]; //won't get the current location right now, so the output will be null NSLog(@"Current Location Longitude: %@", self.longitudeString); NSLog(@"Current Location Latitude: %@", self.latitudeString); } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *newLocation = [locations lastObject]; self.longitudeString = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; self.latitudeString = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } 

位置更新不像您所期望的那样立即提供,您需要等待几秒钟(2-3或更多)才能获得精确的位置更新。 如果你想在viewDidLoad中有位置数据,那么你应该在调用ViewController之前初始化你的位置pipe理器(并调用startUpdatingLocation)(因为不能保证你在viewDidLoad中有位置数据)。