-1

I'm trying to getting my current location exact according to my coordinates. I've implemented CLLocationManager in my viewController called myLocation. My problem is, I'm getting not getting my co-ordinates for the first time, but when I again approach I got the coordinates. I'm unable to understand this problem that why this not appear for the first time. I also tried to give a NSTimer to stoplocation but but still unable to get the result for the first time, every first time I getting a (null) value, and then getting the co-ordinates.

My Code:

#import <UIKit/UIKit.h>
#import <Corelocation/CoreLocation.h>
@interface myLocation : UITableViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@interface myLocation () {
    CLLocationManager* _locationManager;
    NSString * _lat;
    NSString * _lng;
}
@end

@implementation myLocation

- (void)viewDidLoad
{
    [super viewDidLoad];
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [_locationManager requestWhenInUseAuthorization];

    [_locationManager startUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    [_locationManager stopUpdatingLocation];
    _lat =[NSString stringWithFormat:@"%f",location.coordinate.latitude];
     _lng =[NSString stringWithFormat:@"%f",location.coordinate.longitude];
}
- (void)viewWillAppear:(BOOL) animated
{
 NSLOG(@"%@",_lat);   
 NSLOG(@"%@",_lng);       
}
developer
  • 668
  • 1
  • 6
  • 24

2 Answers2

1

Your coordinates aren't appearing yet when you attempt to print them in viewWillAppear: because the CLLocationManager hasn't had enough time to retrieve the first location yet. Wait until didUpdateLocations: is first called before attempting to utilize the device coordinates because didUpdateLocations: is where you'll be receiving those coordinates. I recommend deleting your attempt to print the coordinates code from your viewWillAppear and simply print them in didUpdateLocations: instead.

In the comments, the OP stated he wants to "refresh" the location during viewWillAppear. I suggest stopping the updates when the view disappears and restarting the updates as soon as the view reappears:

- (void)viewWillAppear:(BOOL) animated
{
    [_locationManager startUpdatingLocation];
}

- (void)viewWillDisappear:(BOOL) animated
{
    [_locationManager stopUpdatingLocation];
}
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • Your sugesstion is appreciabble for me, a little bit probelm remains in my secenario. I'm just callling in viewWillAppear so that every time it loads freshly, whenever it just clicked, its all refreshed. a possible scenario, if user haven't internet connection, and after that he got, what can he do to refesh. any suggestions for that ? – developer Dec 26 '14 at 22:44
  • I got the solution, initialization in viewwillapear helped me. Thanks – developer Dec 26 '14 at 22:47
  • 1
    @user3166680 I actually was just typing out a similar suggestion, but no need to completely reinitialize your location manager. I've updated my answer with a suggestion to just stop and restart your locations manager when the view disappears and reappears. – Lyndsey Scott Dec 26 '14 at 22:49
0

It takes some time for location services to start up and call your delegate method - This almost certainly won't happen before viewWillAppear is called if you are only starting location services in viewDidLoad. Also, the first time your app executes it has to wait for the user to grant permission.

You can examine the location property of your CLLocationManager to get the most recent location. If it is nil then no location has been determined (yet).

Paulw11
  • 108,386
  • 14
  • 159
  • 186