Reputation: 823
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>
Upvotes: 0
Views: 319
Reputation: 8511
You also need to enable the services
if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
[CLLocationManager locationServicesEnabled];
}
Upvotes: 0
Reputation: 425
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.
Upvotes: 0
Reputation: 2712
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
Upvotes: 2