简体   繁体   中英

Unable to get longitude and latitude in ios8 by GPS

I am using iOS8 and using the following code for getting longitude and latitude by GPS:

-(void)CurrentLocationIdentifier
{
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse)
    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways)
    {
    // Will open an confirm dialog to get user's approval
    [locationManager requestWhenInUseAuthorization];
    //[_locationManager requestAlwaysAuthorization];
    } else {
    [locationManager startUpdatingLocation]; //Will update location immediately
    }
}

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

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
    case kCLAuthorizationStatusNotDetermined: {
        NSLog(@"User still thinking..");
    }
    case kCLAuthorizationStatusDenied: {
        NSLog(@"User hates you");
    } break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways: {
        [locationManager startUpdatingLocation]; //Will update location immediately
    } break;
    default:
        break;
}
}

After running this code I am getting the output at log is : User still thinking. Can anyone help me with this I am new to iOS and CoreLocation.Framework ? Please help me in finding the Error and how can I solve that.

In iOS 8 you need to do two extra things to get location working: Add a key to your Info.plist and request authorization from the location manager asking it to start. There are two Info.plist keys for the new location authorization. One or both of these keys is required. If neither of the keys are there, you can call startUpdatingLocation but the location manager won't actually start. It won't send a failure message to the delegate either (since it never started, it can't fail). It will also fail if you add one or both of the keys but forget to explicitly request authorization.

So the first thing you need to do is to add one or both of the following keys to your Info.plist file:

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

Both of these keys take a string which is a description of why you need location services. You can enter a string like “Location is required to find out where you are” which, as in iOS 7, can be localized in the InfoPlist.strings file.

Add this string in InfoPlist.strings files

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

Try this code

locationManagerApp=[[CLLocationManager alloc] init];

    locationManagerApp.delegate = self;
    locationManagerApp.distanceFilter = kCLDistanceFilterNone;
    locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {        
           [locationManagerApp requestAlwaysAuthorization];
    }

    [locationManagerApp startUpdatingLocation];
    CLLocation *location1 = [locationManagerApp location];
    CLLocationCoordinate2D coordinate = [location1 coordinate];
    self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
    self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", self.latValue);
    NSLog(@"Longitude = %@", self.longValue);

And run project in device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM