Ios 从NSData方法访问locationManager CLLocation didUpdateLocation数组
我试图通过self.location访问在其方法之外保存更新位置的数组,但这返回的是浮点0.000,而不是纬度。显然,我可以从它自己的方法中获得正确的值 谢谢Ios 从NSData方法访问locationManager CLLocation didUpdateLocation数组,ios,objective-c,cllocationmanager,Ios,Objective C,Cllocationmanager,我试图通过self.location访问在其方法之外保存更新位置的数组,但这返回的是浮点0.000,而不是纬度。显然,我可以从它自己的方法中获得正确的值 谢谢 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.locationManager =
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
self.location = [[CLLocation alloc] init];
NSData* data = [NSData dataWithContentsOfURL:jsonURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
}
- (void)fetchedData:(NSData *)responseData {
//Parse
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSDictionary* coordinates = [json objectForKey:@"jon"];
NSLog(@"Retrieved JSON: %@", coordinates);
NSNumber* coorLat = [coordinates objectForKey:@"latitude"];
NSNumber* coorLon = [coordinates objectForKey:@"longitude"];
float lat = [coorLat floatValue];
float lon = [coorLon floatValue];
self.jsonLat.text = [NSString stringWithFormat:@"%f", lat];
self.jsonLon.text = [NSString stringWithFormat:@"%f", lon];
NSLog(@"%f", self.location.coordinate.latitude); // How to access NSArray *location from locationManager Method????? // ISSUE HERE
//生成位置字典
NSDictionary*myLocation=[NSDictionary Dictionary WithObjectsSandKeys:[NSString stringWithFormat:@%f,3.0],@纬度,[坐标objectForKey:@经度],@经度,零]
// Serialize myLocation Dictionary
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:myLocation
options:NSJSONWritingPrettyPrinted error:&error];
// Print out JSON
self.jsonGenerated.numberOfLines = 0;
self.jsonGenerated.text = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
self.location = locations.lastObject;
self.coordinateLat.text = [NSString stringWithFormat:@"%f", self.location.coordinate.latitude];
self.coordinateLon.text = [NSString stringWithFormat:@"%f", self.location.coordinate.longitude];
self.altitude.text = [NSString stringWithFormat:@"%f", self.location.altitude];
self.hAccuracy.text = [NSString stringWithFormat:@"%f", self.location.horizontalAccuracy];
self.vAccuracy.text = [NSString stringWithFormat:@"%f", self.location.verticalAccuracy];
self.timestamp.text = [NSString stringWithFormat:@"%@", self.location.timestamp];
self.speed.text = [NSString stringWithFormat:@"%f", self.location.speed];
self.course.text = [NSString stringWithFormat:@"%f", self.location.course];
}
DidUpdateLocation在您开始更新位置后不久调用,但在startUpdatingLocation返回之前不会调用
如果您检查self.location属性,您会发现它为零
您应该在didUpdateLocations中调用web服务调用,但显然,您需要确定一种策略来决定何时请求新数据。需要考虑的一些事项:
您可以使用移动筛选仅在位置发生一定程度的更改时接收更新
初始更新后,如果不需要详细的位置信息,您可以切换到重要的位置更改服务
还可以比较当前和以前的“水平精度”值,以确定此更新是否比上次更新更精确
另外,请注意,初始位置可能不太准确,因此您可能需要等待一些更新才能获得准确信息您是否在等待locationManager:didUpdateLocations:调用?我会[self.locationManager startUpdatingLocation];然后,在下面几行完成获取数据。startUpdatingLocation不同步;在第一次调用locationManager:didUpdateLocations:之前,该位置不一定可用。在这个方法中调用了didUpdateLocations:不是吗-voidlocationManager:CLLocationManager*管理器didUpdateLocations:NSArray*位置{在viewDidLoad期间使用:self.locationManager=[[CLLocationManager alloc]init]初始化该位置;DidUpdateLocation在您开始更新位置后不久调用,但在startUpdatingLocation返回之前不会调用。如果检查self.location,您会发现它为零。您应该在didUpdateLocations中调用Web服务调用。另外,请注意,初始位置可能不太准确,因此您可能需要等待在您获得准确信息之前进行一些更新