位置pipe理器给出空坐标

以下代码导致null坐标。 奇怪的是UIAlert提示应用程序使用当前位置出现之前,用户可以select是的。

我使用的代码:

CLLocationManager *locationManager; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; locationManager = [[CLLocationManager alloc] init]; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; [locationManager startUpdatingLocation]; float latitude = locationManager.location.coordinate.latitude; float longitude = locationManager.location.coordinate.longitude; NSLog(@"%.8f",latitude); NSLog(@"%.8f",longitude); 

NSLog为两个坐标打印0.0000000

谢谢!

你得到0的原因是因为位置pipe理器没有收集到任何数据(它已经开始思考)

您需要将您的class级设置为位置pipe理器的代理(即提供一个在检索到新位置时调用的函数),并保留您的位置pipe理器。

 // Inside .m file @interface MyClass () <CLLocationManagerDelegate> // Declare this class to implement protocol CLLocationManagerDelegate @property (strong, nonatomic) CLLocationManager* locationManager; // Retains it with strong keyword @end @implementation MyClass // Inside some method self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = kCLDistanceFilterNone; [self.locationManager startUpdatingLocation]; // Delegate method - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation* loc = [locations lastObject]; // locations is guaranteed to have at least one object float latitude = loc.coordinate.latitude; float longitude = loc.coordinate.longitude; NSLog(@"%.8f",latitude); NSLog(@"%.8f",longitude); }