0

I'm trying to get user current location.Here is my code:

-(CLLocationCoordinate2D) getLocation{
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];
        CLLocation *location = [locationManager location];
        CLLocationCoordinate2D coordinate = [location coordinate];

        return coordinate;
}

and this is where i call it in viewDidLoad

CLLocationCoordinate2D coordinate = [self getLocation];
double latitude = coordinate.latitude;
double longitude = coordinate.longitude;

and it return 0.000000 for both latitude and longitude. Any help please! I'm using Xcode 8 and running on My real iPhone 6s not Emulator.

sann chhailong
  • 53
  • 2
  • 10

1 Answers1

4

You can use this code:

 - (void)viewDidLoad 
    {
       [super viewDidLoad];
       locationManager = [[CLLocationManager alloc] init];
       locationManager.delegate = self;
       locationManager.distanceFilter = kCLDistanceFilterNone;
       locationManager.desiredAccuracy = kCLLocationAccuracyBest;

       if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
                        [self.locationManager requestWhenInUseAuthorization];

       [locationManager startUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
     {
        CLLocation *location = [locations lastObject];
        NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
    }
Nidhi Patel
  • 1,222
  • 11
  • 17