无法将用户的位置存储在变量中

时间:2013-09-20 16:30:28

标签: ios xcode facebook-graph-api

- (IBAction)getdirections:(id)sender {
    directionview=[[directionViewController alloc]initWithNibName:@"directionViewController" bundle:nil];
    directionview.sourceplace=user.location;
    [self.view addSubview:directionview.view];
}

我想将用户的位置存储在“sourceplace”中,这是另一个名为“NSString”的视图控制器中的directionViewController类型变量。 我从变量“user”中获取数据,我尝试NSLogging用户,我可以看到它有“位置”键。

1 个答案:

答案 0 :(得分:0)

在你的.h:

#import <CoreLocation/CoreLocation.h>

@interface yourController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
    NSString *str;
}

在你的.m:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

您可以将位置转换为字符串,如下所示:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f", location.coordinate.latitude, location.coordinate.longitude];
    NSLog(@"%@",str);    
}

之后

如果您的directionview.sourceplace也是NSString

directionview.sourceplace = str;