Ios 核心位置可以';无法在viewDidLoad中获取数据

Ios 核心位置可以';无法在viewDidLoad中获取数据,ios,core-location,cllocationmanager,Ios,Core Location,Cllocationmanager,我编写了一个简单的iOS应用程序,检索位置信息,然后使用该位置请求Yahoo Weather 问题是,即使我在viewDidLoad中调用核心位置,它也不会立即给出结果 那为什么我不能得到位置信息呢 如何在viewDidLoad中获取位置信息 伪代码当前类似于: - (void)viewDidLoad { [super viewDidLoad]; self.locManager = [[CLLocationManager alloc] init]; self.locMa

我编写了一个简单的iOS应用程序,检索位置信息,然后使用该位置请求Yahoo Weather

问题是,即使我在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之前初始化位置管理器(并调用startUpdatingLocation)(因为不保证在viewDidLoad中包含位置数据)

等待时间未定义。这可能需要2-3秒以上的时间。而且只是初始化也没用——你必须给startUpdate打电话