OStack程序员社区-中国程序员成长平台

标题: ios - 未检索到纬度和经度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:31
标题: ios - 未检索到纬度和经度

iOS9 更新:我是 iOS 新手,正在尝试使用 CoreLocation 来返回纬度和经度值。以下代码从不执行日志输出

 -(void)viewWillAppearBOOL)animated{

manager = [[CLLocationManager alloc]init];

manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;

[manager startUpdatingLocation];
}

这是我的 didUpdateToLocation 函数,但从未达到

- (void)locationManagerCLLocationManager *)manager
 didUpdateLocationsNSArray<CLLocation *> *)locations{

NSLog(@"Location: %@", locations);
CLLocation *currentLocation = locations.lastObject;

if (currentLocation != nil) {
    float latitude = currentLocation.coordinate.latitude;

    float longitude = currentLocation.coordinate.longitude;

    NSLog(@"dLongitude : %f", longitude);
    NSLog(@"dLatitude : %f", latitude);
}

else{ NSLog(@"ERROR");
    }

}



Best Answer-推荐答案


CLLocationManager是异步的,你应该在委托(delegate)方法中获取经纬度。

locationManager.delegate = self;

然后实现这个委托(delegate)方法:

- locationManager:didUpdateLocations:

关于ios - 未检索到纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192331/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4