How to get current location latitude and longitude in IOS SDK 8.0

11,351

Solution 1

I figure it out issue in IOS 8.0

NSLocationAlwaysUsageDescription add this manually into application plist file, leave string value empty. This it will call the delegate methods. Never forgot about adding NSLocationAlwaysUsageDescription unless you not added this delegate method will not work. Remaining code is same. with adding delegate methods to controller class.

add this line of code in locationManager declaration code. for IOS 8.0 [locationManager requestAlwaysAuthorization];

Solution 2

In iOS 8, this code fails silently i.e you won’t get any error or warning.

You need to do two extra things to get location working:

  1. Add a key to your Info.plist
  2. Request authorization from the location manager asking it to start.

Any one or both of below keys needs to be added in Info.plist file.

  1. NSLocationWhenInUseUsageDescription
  2. NSLocationAlwaysUsageDescription

These are of String type and value can be any message description or empty.

Now you need to request authorisation for corresponding location method. Use any one of below calls :

  1. [self.locationManager requestWhenInUseAuthorization]
  2. [self.locationManager requestAlwaysAuthorization]
Share:
11,351
KkMIW
Author by

KkMIW

Updated on July 06, 2022

Comments

  • KkMIW
    KkMIW almost 2 years

    how to get current location latitude and longitude.

    I have try this. Using Xcode 6.01 and IOS SDK 8.0

    -(CLLocationCoordinate2D) getLocation{
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];
        CLLocation *location = [locationManager location];
        CLLocationCoordinate2D coordinate = [location coordinate];
        return coordinate;
    }
    
    - (void)getCurrentLocation{    
        CLLocationCoordinate2D coordinate = [self getLocation];
        NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
        NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
        NSLog(@"Latitude  = %@", latitude);
        NSLog(@"Longitude = %@", longitude);
    }
    
    
    - (void)viewDidLoad{
        [super viewDidLoad];
        [self getCurrentLocation];
    
    }
    

    Here I enable the simulator location to Deployment 8.0 iPhone simulator 4s. Even on device its not working.

    @ALL Please let me know, what i have done wrong here.

  • KkMIW
    KkMIW over 9 years
    added both delegate method didFailWithError and didUpdateToLocation but this methods are not triggering at all
  • Samin
    Samin over 9 years
    please add CLLocationManagerDelegate in your .h file like this @interface DemoViewController : UIViewController<CLLocationManagerDelegate>
  • KkMIW
    KkMIW over 9 years
    I added CLLocationManagerDelegate, still i have issue. there is something wrong after upgrade sdk IOS 7 to IOS 8.