1

I'm Using CLLocationManager to get the current location of the device and i tried to get the location property in order to get the longitude and latitude as follows:

-(void)getCurrentLocation{
CLLocationManager *manager=[[CLLocationManager alloc]init];;
manager.delegate=self;
manager.desiredAccuracy=kCLLocationAccuracyBest;
self.currentLocation=manager.location;
NSLog(@"Current Lat :%f",self.currentLocation.coordinate.latitude);
NSLog(@"Current Long :%f",self.currentLocation.coordinate.longitude);

[manager startUpdatingLocation];
}

Given that:

self.currentLocation is a property inside my class (Which is the CLLocationManagerDelegate) as follows:

.h

@property(nonatomic,strong) CLLocation *currentLocation;

and the getter in the .m is as follows:

-(CLLocation *)currentLocation{
if (!_currentLocation) {
    _currentLocation=[[CLLocation alloc]init ];
}
return _currentLocation;

}

I forget to say that i have implemented the didUpdateToLocation Method as follows:

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation {
NSLog(@"didUpdateToLocation");
CLLocation *loc=newLocation;
if (loc!=nil) {
    self.currentLocation=loc;

}  

}

I also tried to put this statement after the startUpdateLocation call:

    self.currentLocation=manager.location;

the problem is that when i call the previous function getCurrentLocation the two NSLogs inside it prints 0.000000 which means that manager.location isn't working and the weird thing is that the first NSLog inside didUpdateToLocation doesn't printed , thanks in advance

Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165
  • You should implement the `CLLocationManagerDelegate` methods (notably, `locationManager:didUpdateLocations:`) and wait until you get called there. You should view this process of starting location manager and waiting for responses as an asynchronous process. – Rob Feb 11 '14 at 15:51
  • Please note that didUpdateToLocation is deprecated as of iOS 6.0! Use didUpdateLocations as in my example instead. Your problem is that your delegate method is not called until a later point in time. Could be 10 ms, or a few seconds. Depends on the GPS signal etc. Try and move your NSLog calls to the delegate method as implemented in my exmaple below. – MartinHN Feb 12 '14 at 10:15

1 Answers1

2

You can't just read the location from the CLLocationManager. It needs to update its location before that property is assigned:

The value of this property is nil if no location data has ever been retrieved.

CLLocationManager SDK

You must implement the delegate method locationManager:didUpdateLocations:

And when that is called you can read the location property, or look at the locations argument:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    self.currentLocation = [locations firstObject];

    NSLog(@"Current Lat :%f",self.currentLocation.coordinate.latitude);
    NSLog(@"Current Long :%f",self.currentLocation.coordinate.longitude);

    [manager stopUpdatingLocation]
}

You can call [manager stopUpdatingLocation] when you get the first call, so it doesn't keep running.

MartinHN
  • 19,542
  • 19
  • 89
  • 131
  • @JavaPlayer Your problem is still that you're trying to read the location before it has been retrieved from the GPS. And furthermore, you're using a deprecated delegate method, as explained in my comment above. Try to implement my delegate method above and see what happens. – MartinHN Feb 12 '14 at 10:18
  • I tried that and it's working well on simulator but it's not working on the device (the didUpdateLocations method isn't called !!) although i'm turning on the location service of the device – Eslam Hamdy Feb 12 '14 at 11:14
  • Try to implement locationManager:didFailWithError: and see if there are any errors. If it works in th simulator, and not on the device something must be going wrong. – MartinHN Feb 12 '14 at 11:34
  • locationManager:didFailWithError: also doesn't working on device !! – Eslam Hamdy Feb 12 '14 at 12:03
  • Are you sure location services are enabled on the device? Try to call `[CLLocationManager authorizationStatus]` and `[CLLocationManager locationServicesEnabled]` - and see if they return `kCLAuthorizationStatusAuthorized` and `YES` respectively. – MartinHN Feb 12 '14 at 13:32
  • Error:Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)", didFailWithError is called but i didn't note that (as it's executed after long time) and the previous error is raised – Eslam Hamdy Feb 12 '14 at 16:47
  • Give me some context please. Device? OS version? On wifi? This error seems specific to the simulator. – MartinHN Feb 12 '14 at 20:45
  • Check this: http://stackoverflow.com/questions/6032976/didfailwitherror-error-domain-kclerrordomain-code-0-the-operation-couldnt-be – MartinHN Feb 12 '14 at 20:48