Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager startUpdatingLocation not working

So now I'm at least getting callbacks with the following code...

- (void)viewDidLoad {

[super viewDidLoad];
mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
//mapView.showsUserLocation=TRUE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];

NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
    CLLocationManager *newLocationManager = [[CLLocationManager alloc] init];
    [newLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [newLocationManager setDistanceFilter:kCLDistanceFilterNone];
    [self setLocationManager:newLocationManager];


[[self locationManager] setDelegate:self];
[[self locationManager] startUpdatingLocation];
NSLog(@"Started updating Location");

}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

NSLog(@"Did update to location");
mStoreLocationButton.hidden=FALSE;
location=newLocation.coordinate;

MKCoordinateRegion region;
region.center=location;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;


[mapView setRegion:region animated:TRUE];


}

I can set breakpoints in the second method and NSLog is reporting continual location updates, but for some reason the zoom with span isn't working. Any idea why? It's got my coordinates and everything. Sort of scratching my head on this one.

like image 275
Coltrane Avatar asked Jul 28 '12 02:07

Coltrane


People also ask

What does startUpdatingLocation do?

startUpdatingLocation() Starts the generation of updates that report the user's current location.

What is Cllocationmanagerdelegate?

The methods that you use to receive events from an associated location-manager object. iOS 2.0+ iPadOS 2.0+ macOS 10.6+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


2 Answers

Assign the CLLocationManager to a (strong) property on your class. (I assume you're using ARC BTW.) Right now the CLLocationManager doesn't live past the end of the viewDidLoad method, so it won't get to call your delegate method either.

like image 126
Johan Kool Avatar answered Sep 28 '22 00:09

Johan Kool


Make sure that you've added <CLLocationManagerDelegate> in the @interface file.

Edit:

If the delegate is set properly, make sure you're using your locationManager property:

In the .h file:

@property (nonatomic, strong) CLLocationManager *locationManager;

In viewDidLoad:

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
like image 27
nevan king Avatar answered Sep 28 '22 01:09

nevan king