I need to find my current location with CoreLocation, I tried multiple methods but so far my CLLocationManager has only returned 0’s.. (0.000.00.000).
Here’s my code (updated to work):
Imports:
#import <CoreLocation/CoreLocation.h>
Declared:
IBOutlet CLLocationManager *locationManager;
IBOutlet UILabel *latLabel;
IBOutlet UILabel *longLabel;
Functions:
- (void)getLocation { //Called when needed
latLabel.text = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
longLabel.text = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];
}
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
You can find your location using
CoreLocationlike this:import
CoreLocation:Declare
CLLocationManager:Initialize the
locationManagerinviewDidLoadand create a function that canreturnthe current location as anNSString:And calling the
deviceLocationfunction will return the location as expected:This is just an example. Initializing
CLLocationManagerwithout the user being ready for it isn’t a good idea. And, of course,locationManager.location.coordinatecan be used to getlatitudeandlongitudeat will afterCLLocationManagerhas been initialized.Don’t forget to add the
CoreLocation.frameworkin your project settings under the Build Phases tab (Targets->Build Phases->Link Binary).