iOS保存纬度/经度坐标到NSString以输出到标签

iOS保存纬度/经度坐标到NSString以输出到标签,ios,cllocationmanager,cllocation,Ios,Cllocationmanager,Cllocation,如何将纬度和经度坐标保存到nsstring,以便在其他函数中使用。基本上,我只想显示从要传递给uilabel的坐标中检索到的值 - (void)viewDidLoad { [super viewDidLoad]; [self getCurrentLocation]; NSLog(@"lat is %@ : lon is %@",self.latPoint, self.longPoint); } 我试图用NSlog检索上面的内容,结果显示为null。我在.h文件中创建了两个NSString属性

如何将纬度和经度坐标保存到nsstring,以便在其他函数中使用。基本上,我只想显示从要传递给uilabel的坐标中检索到的值

- (void)viewDidLoad
{
[super viewDidLoad];
[self getCurrentLocation];
NSLog(@"lat is %@ : lon is %@",self.latPoint, self.longPoint);

}
我试图用NSlog检索上面的内容,结果显示为null。我在.h文件中创建了两个NSString属性,即latPoint/longPoint

- (void)getCurrentLocation {
if ([CLLocationManager locationServicesEnabled]) {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
} else {
    NSLog(@"Location services are not enabled");
}

}



- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
self.latPoint = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
self.lonPoint = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
}

您看到的行为很可能是因为
CLLocationManager
回调是异步的。您对打印
self.latPoint
self.longPoint
(很可能)的NSLog调用发生在位置管理器找到并存储当前位置之前

如果移动
NSLog(@“lat is%@:lon is%@”、self.latPoint、self.longPoint)didUpdateLocations
方法调用code>语句,那么您应该看到,只要位置管理器找到(并更新)您的当前位置,就会调用它

您只需要对
CLLocationManager
回调作出反应,而不是试图假设何时找到位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    self.latPoint = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
    self.lonPoint = [NSString stringWithFormat:@"%f", location.coordinate.longitude];

    NSLog(@"lat is %@ : lon is %@",self.latPoint, self.longPoint);
    //Now you know the location has been found, do other things, call others methods here
}

John

最有可能是因为位置管理器是异步的,并且在位置管理器有时间查找和存储当前位置之前,您对print selt.latpoint和self.longpoint的NSLog调用就发生了。将NSLog语句移动到didUpdateLocations方法。如何将值存储到nsstring,以便在其他方法中使用它?如何在ViewDidLoad中找到坐标后重新激活它?我是mapkit的新手。请帮助这是core location而不是mapkit,您试图重新激活什么?抱歉,我的意思是core location,如何将坐标保存到nsstring并在view加载时使用它