0

I made necessary changes for my app according to iOS8.1 SDK to track location. I am using below piece of code to get latitude and longitude.Eventhough i am not getting latitude and longitude values. Should i need to do any changes other than this to get latitude and longitude values?

In appdelegate.h:

CLLocationManager *locationManager;

In appdelegate.m:

- (void)applicationDidBecomeActive:(UIApplication *)application{
    if (locationManager==nil) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [locationManager requestAlwaysAuthorization];
        }
        [locationManager startUpdatingLocation];
    }

    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSLog(@" location details %f %f ",locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
}

In Info.plist file

 <key>NSLocationAlwaysUsageDescription</key>
<string>This will be used to obtain or track your location.</string>
Avinash
  • 823
  • 1
  • 7
  • 18

3 Answers3

2

I believe you are not making use of the CLLocationManagerDelegate to be informed when your CLLocationManager actually has retrieved a location. Attempting to get a location from your Location Manager straight away will almost certainly return no value everytime because you only just requested it to start to update.

What you need to do is the following:

1) Declare your appdelegate class as your CLLocationManager's delegate in appdelegate.m.

self.locationManager = self;

2) Tell your appdelegate.h file to adopt the CLLocationManagerDelegate Protocol

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

3) Implement the locationManager:didUpdateLocations method provided by CLLocationManagerDelegate in appdelegate.m

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { }

4) Get the locations your looking for in the method you just implemented. :)

I recommend you take a look at the documentation Apple provides as since you are using C-Style dereferencing operators I suspect you are a newcomer to the Objective-C language

Darryl Bayliss
  • 2,677
  • 2
  • 19
  • 28
  • can't i get latitude and longitude values without CLLocationManagerDelegate? – Avinash Nov 05 '14 at 13:00
  • It's possible but not recommended as this will take many seconds as stated in the documentation. https://developer.apple.com/Library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/startUpdatingLocation It's highly recommended to make use of the delegate methods provided so you know when CLLocationManager has finished updating. – Darryl Bayliss Nov 05 '14 at 13:04
0

Did you check the permissions from Settings ? You should expect that location to be delivered to you in the delegate methods. After the user has accespted. Otherwise i'm not sure the manager will have a location on the spot.

0

You also need to enable the services

if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
    [CLLocationManager locationServicesEnabled];
}
Flexicoder
  • 8,251
  • 4
  • 42
  • 56