0

I have been trying to get the current location IOS8 and following thread Location Services not working in iOS 8 but getting the 00.00,00.00 instead current location. Any suggestion on where I am doing wrong.

ViewController.h

 #import <UIKit/UIKit.h>
 #import <CoreLocation/CoreLocation.h>

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

 @interface ViewController : UIViewController <CLLocationManagerDelegate>

 @property (strong, nonatomic) CLLocationManager *locationManager;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

 - (void)viewDidLoad {

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);


CLLocation *location = [self.locationManager location];

// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];


NSLog(@"%@",latitude);
NSLog(@"%@",longitude);

[super viewDidLoad];

}

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}

 -(void)setUpMap
 {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];

 }



 -(NSString *)deviceLocation 
 {
   return [NSString stringWithFormat:@"latitude: %f longitude: %f",         self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
 }
  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 {
   [manager stopUpdatingLocation];

CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];

float latitude_current = newLocation.coordinate.latitude;
float longitude_current = newLocation.coordinate.longitude;


NSLog(@"%f",latitude_current);
NSLog(@"%f",longitude_current);

NSLog(@"Current latitude :%f",coordinate_currentlocation.latitude);
NSLog(@"Current longitude :%f",coordinate_currentlocation.longitude);
} 
@end

I have also added the following key in my info.plist

<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is needed for this app.</string>
Community
  • 1
  • 1
Santosh
  • 202
  • 3
  • 16

2 Answers2

0

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 your project in device. If you run project in simulator then not get lat/ long.

get the current location on simulator (select any one location). enter image description here

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
  • Forgot to mention that I am getting the coordinate in device but wanted to test it other workflow once i get the coordinate on simulator. – Santosh Jun 08 '15 at 12:15
0

There was a mistake in code, The function which actually checking the authorization never called. I moved my all the code to viewDidLoad as below and it worked. Thank you for looking into this.

- (void)viewDidLoad {

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

#ifdef __IPHONE_8_0
        if(IS_OS_8_OR_LATER)
        {
            [self.locationManager requestAlwaysAuthorization];
        }
#endif
            [self.locationManager startUpdatingLocation];
            [self.locationManager startUpdatingLocation];

            NSLog(@"%@", [self deviceLocation]);

[super viewDidLoad];
}
Santosh
  • 202
  • 3
  • 16