0

I am using the below code to get coordinates on click of a button

-(CLLocationCoordinate2D) getLocation{
    CLLocationCoordinate2D coordinate;
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    if ([CLLocationManager locationServicesEnabled]) {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];
        [locationManager startUpdatingLocation];
        CLLocation *location = [locationManager location];
        coordinate = [location coordinate];
    }
    else {
        coordinate.latitude = 0.0;
        coordinate.longitude = 0.0;
    }

    return coordinate;
}

When the button is clicked for the first time, I do get my valid coordinates, but if I click the button again, The latitude and longitude values are 0.0000 Any suggestions

  • When you debug this in XCode, what happens? Is it that the `else` clause is invoked because `[CLLocationManager locationServicesEnabled]` evaluates to `false`? Or is it because `[location coordinate]` returns `0,0` ? Are you sure that `location` isn't null when it's returned from `[locationManager location]` ? – selbie Nov 13 '16 at 18:02
  • Also, the docs imply you should only instantiate one CLLocationManager instance for the entire app. You appear to be creating a new one on every button click. – selbie Nov 13 '16 at 18:04
  • I confirmed that the if condition satisfied during debug.. – user1966460 Nov 13 '16 at 18:21
  • Thank you selbie for the inputs, I confirmed that the if condition satisfied during debug.. during second click. My target is to create a library, which calls a sequence of methods, one of which is to get location. In the sample app when a button is clicked, I am calling the methods in sequence.. How to create only one CLLocationManager for the app in library? – user1966460 Nov 13 '16 at 18:28
  • http://stackoverflow.com/questions/13263229/objective-c-instance-variables – selbie Nov 13 '16 at 18:54

2 Answers2

0

Please initialize the CLLocationManager outside the method.Take the You can initialize in viewdidload method. It may helps to you

Sanjukta
  • 1,057
  • 6
  • 16
0

You cannot expect [locationManager location] to return anything useful immediately after activating a location manager with [locationManager startUpdatingLocation] .

CLLocationManager takes time to acquire a location. You need to set the CLLocationManager's delegate (eg, to 'self') and then use the delegate method - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations; to get notified when the location manager has got a location that you can use.

See: https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate/1423615-locationmanager?language=objc

(If you only want one location, and not a continuous stream of location updates, instead of calling 'startUpdatingLocation' call [locationManager requestLocation] - but even then you still need to use the delegate method to be notified when a location has been acquired).

You need to split out the code for creating and starting the location manager to where it is only run once (only need one location manager for the app) - eg, viewDidLoad. Then the rest of the code, to process the locations acquired, should go in the delegate method.

In reality, if you want you button click to have a location immediately, you need to have your location manager running and updating locations before your button is available to be clicked. Eg, make your button enabled=NO and when you start getting locations via the delegate method, set the button enabled=YES.

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29