Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Longitude/Latitude of the User When the viewDidLoad

My purpose is to get the longitude/latitude of the user when the view is loaded and to store the values in two variables for later calculations. I don't need to track the user location and to update it, I simply need to get his/her coordinates once. My code looks like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    // locationManager update as location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    float longitude=coordinate.longitude;
    float latitude=coordinate.latitude;

    NSLog(@"dLongitude : %f",longitude);
    NSLog(@"dLatitude : %f", latitude); 
}

In the console i keep getting this:

dLongitude : 0.000000
dLatitude : 0.000000

Can you please help me there?

like image 534
Luca Avatar asked May 16 '11 11:05

Luca


3 Answers

To get user's location even once you need to make locationManager to start updating locations (you did that) and implement delegate method that manager calls when location is retrieved - you can't get location from location manager immediately.

If you don't want to track user location - just stop updating locations in that delegate method after location was fetched for the 1st time (and store it if you need it in future).

like image 98
Vladimir Avatar answered Oct 25 '22 12:10

Vladimir


As soon as you call [locationManager startUpdatingLocation]; your viewDidLoad method is done. You need to implement

-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation 
           fromLocation: (CLLocation *) oldLocation

and

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

which will be called by your locationManager when it gets the location, or when it fails. In both of those methods, you can call [locationManager stopUpdatingLocation].

Inside the didUpdateToLocation method, you should move all your code from viewDidLoad, starting with CLLocation *location = [locationManager location]; That's where you'll get proper values.

like image 41
Joey Gibson Avatar answered Oct 25 '22 12:10

Joey Gibson


This worked for me

- (void)viewDidLoad {
    [super viewDidLoad];
    // locationManager update as location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    [locationManager stopUpdatingLocation];
    CLLocation *location = [locationManager location];
    // Configure the new event with information from the location

    float longitude=location.coordinate.longitude;
    float latitude=location.coordinate.latitude;

    NSLog(@"dLongitude : %f", longitude);
    NSLog(@"dLatitude : %f", latitude); 
}
like image 34
Fred Avatar answered Oct 25 '22 11:10

Fred