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.
startUpdatingLocation() Starts the generation of updates that report the user's current location.
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+
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With