I am trying to access the array that holds the updated location outside its method via self.location but this is returning the float 0.000 instead of the latitude. I have no problem getting the correct value from within its own method obviously.
Thanks.
@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
//Generate Location Dictionary NSDictionary *myLocation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f", 3.0],@"latitude", [coordinates objectForKey:@"longitude"],@"longitude", nil];
// 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];
}
startUpdatingLocation
is not synchronous; the location won't necessarily be available untillocationManager:didUpdateLocations:
is called for the first time. – Ryan Dignard-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
Which is initialized during viewDidLoad with:self.locationManager = [[CLLocationManager alloc] init];
– MyelindidUpdateLocations
is called soon after you start updating location, but it is not called beforestartUpdatingLocation
returns. If you check yourself.location
you will find that it is nil. You should invoke your Web services call indidUpdateLocations
. Also, be aware that the initial location may not be very accurate,so you may need to wait for a few updates before you get accurate information – Paulw11