我正在使用 CLLocationManager 来获取用户的当前位置。我需要用户的当前位置,因为无论用户在哪里,我都使用 Open Weather Api 来显示天气数据。我的问题是我第一次打开应用程序时,经纬度值是 0、0。但是如果我再次运行应用程序,它就可以正常工作。因此,只有在应用程序首次启动时才使用不正确的 lat 和 long 值。我在 viewDiDLoad 中完成了所有设置,这是代码...
locationManager = [[CLLocationManager alloc] init];
[locationManager requestAlwaysAuthorization];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self->locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
我不知道我做错了什么。这与在视图控制器而不是 App Delegate 中获取 lat 和 long 有关吗?
这是我获取位置值的方法:
locationManager.location.coordinate.latitude
locationManager.location.coordinate.longitude