0
votes

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];
 }
1
Are you waiting until locationManager:didUpdateLocations: is called?Ryan Dignard
Well I do [self.locationManager startUpdatingLocation]; then fetchedData is done a few lines down.Myelin
startUpdatingLocation is not synchronous; the location won't necessarily be available until locationManager:didUpdateLocations: is called for the first time.Ryan Dignard
Well the didUpdateLocations: is called during this method isn't it? -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { Which is initialized during viewDidLoad with: self.locationManager = [[CLLocationManager alloc] init];Myelin
didUpdateLocations is called soon after you start updating location, but it is not called before startUpdatingLocation returns. If you check your self.location you will find that it is nil. You should invoke your Web services call in didUpdateLocations. 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 informationPaulw11

1 Answers

0
votes

didUpdateLocations is called soon after you start updating location, but it is not called before startUpdatingLocation returns.

If you check your self.location property you will find that it is nil.

You should invoke your web services call in didUpdateLocations, but obviously you will need to determine a strategy for deciding when to request new data. Some things to consider:

  • You can use movement filtering to only receive updates when location has changed by a certain amount
  • After an initial update you can switch to the Significant Location Change service, if you don't need fine location detail
  • You can also compare the current and previous horizontalAccuracy values to determine if this update is more accurate than the last

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