Search code examples
xcodenulllocationcore-locationnslog

how to avoid nulls while getting current location?


I want to get users current location and i found that solution in stackoverflow: How to find your current location with CoreLocation. but in spite of that solution i get that message on Nslog: 2013-05-07 23:57:56.962 XXXXX[1951:c07] Latitude:(null),Longitude:(null)

Here is what i have done:

In header file:

#import <CoreLocation/CoreLocation.h>
NSString *UsersCurrentLatitude;
NSString *UsersCurrentLongitude;
CLLocationManager *locationManager;

in implemention:

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

        UsersCurrentLongitude:[NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];
        UsersCurrentLatitude:[NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];

            locationManager = [[CLLocationManager alloc] init];
            locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
            locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
            [locationManager startUpdatingLocation];

        NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);

}

Solution

  • You are missing a step here, you need to start the location service then get the position from the delegates. You might want to take a look at an exemple from Apple or at this tutorial.