Reputation: 11
Hi I am having trouble using CLLocationManager to find the current user location. Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
- (IBAction)pressed:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(@"%.8f", currentLocation.coordinate.longitude);
NSLog(@"%.8f", currentLocation.coordinate.latitude);
}
}
When I run this code, nothing happens at all. There is no reaction in the debugger. As I understand, didUpdateToLocation is deprecated in iOS6 but I thought it was still usable. If anyone can tell me what I am doing wrong or suggest an alternative please let me know. Thanks!
Upvotes: 0
Views: 416
Reputation: 91
How'd you define stuff in your .h file? I assume like:
#import <UIKit/UIKit.h>
#import <CoreLocation/CLLocationManager.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
And the main .m file I ran:
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
return YES;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(@"%.8f", currentLocation.coordinate.longitude);
NSLog(@"%.8f", currentLocation.coordinate.latitude);
}
}
Here's the output on the simulated iOS:
2014-08-12 13:36:28.933 tester[5577:60b] Application windows are expected to have a root view controller at the end of application launch
2014-08-12 13:36:35.237 tester[5577:60b] didUpdateToLocation: <+37.78******,-122.40******> +/- 5.00m (speed -1.00 mps / course -1.00) @ 8/12/14, 1:36:35 PM Central Daylight Time
2014-08-12 13:36:35.237 tester[5577:60b] -122.40******
2014-08-12 13:36:35.238 tester[5577:60b] 37.78******
Upvotes: 1