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.
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:In
viewDidLoad
:For Swift 3
The method has changed to:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
from:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)
Notice the '_' and the cast of 'locations' to [CLLocation] rather than [AnyObject]!
If you use the old method it will never be called and you won't receive a warning that it has changed.
In my case, it didn't enter on didChangeAuthorization. I was trying on a real device.
I deleted the application from the phone and install it again. And it works!
Hope this helps someone :)
Well, first of all, you can never be sure that the location manager is able to update the location in the first place. There could be an error during update or you don't have access to the user's location.
Implement this CLLocationManager delegate method and verify the error.
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.
If you're running this in the simulator only you might need to prompt it to change coordinates. In Xcode there is a bar above the debug output pane with the typical Location Services arrow. Next to that is a drop down list of locations. Once your app is running, switch the location it is simulating and see if that change triggers your code. Then test it on a real device.