CLLocationManager startUpdatingLocation not working

So, now I at least get 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 reports constant location updates, but for some reason the scale with the range does not work. Any idea why? He got my coordinates and all that. Scratching my head on this.

+8
ios iphone cllocationmanager mkmapview zoom
source share
7 answers

Assign a CLLocationManager to a (strong) property in your class. (I assume you are using ARC BTW.) Currently, the CLLocationManager does not live until the end of the viewDidLoad method, so it will also not be able to call your delegation method.

+15
source share

Make sure you add <CLLocationManagerDelegate> to the @interface file.

Edit

If the delegate is set correctly, make sure you use the 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]; 
+14
source share

I think you can do this work in two ways:

  • Using the CLLocation Environment

Make sure you accept ViEWController using the CLLocationManagerDelegate methods

 #import<MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> { CLLocationCoordinate2D location; MKMapView *mapView; } @end 

In ViewController.m:

 @implementation GSViewController - (void)viewDidLoad { [super viewDidLoad]; mapView=[[MKMapView alloc] initWithFrame:self.view.frame]; mapView.showsUserLocation=TRUE; mapView.delegate=self; [self.view insertSubview:mapView atIndex:0]; CLLocationManager *locationManager=[[CLLocationManager alloc] init]; locationManager.delegate=self; locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; [locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ NSLog(@"new location: %@", newLocation); 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]; } -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"error: %@", error.description); } @end 

2. Using the same MKMapKit structure You can do this using the MKMapViewDelegate method called didUpdateUserLocation: Here you do not need CLLocaionManager, This will be done: In ViewController.h:

 #import <MapKit/MapKit.h> @interface ViewController : UIViewController < MKMapViewDelegate> { CLLocationCoordinate2D location; MKMapView *mapView; } @end 

and in the ViewController.m file:

 @implementation GSViewController - (void)viewDidLoad { [super viewDidLoad]; mapView=[[MKMapView alloc] initWithFrame:self.view.frame]; mapView.showsUserLocation=TRUE; mapView.delegate=self; [self.view insertSubview:mapView atIndex:0]; } -(void)mapView:(MKMapView *)mapV didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"map new location: %f %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude); location=userLocation.coordinate; MKCoordinateRegion region; region.center=location; MKCoordinateSpan span; span.latitudeDelta=0.1; span.longitudeDelta=0.1; region.span=span; [mapV setRegion:region animated:TRUE]; } @end 
+5
source share

Well, firstly, you can never be sure that a location manager will be able to update a location in the first place. There may be an error during the update, or you do not have access to the user's location.

Implement this CLLocationManager delegation method and check for the error.

 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 

"Implementing this method is optional. However, you must implement this method."

+1
source share

If you use this in a simulator, you may need to change its coordinates. Xcode has a panel above the debug output area with a typical arrow of location services. Nearby is a drop-down list of locations. Once the application is running, switch the location that it simulates and see if this change causes your code. Then test it on a real device.

+1
source share

In my case, it was not part of hasChangeAuthorization. I tried to use a real device.

I uninstalled the application from the phone and installed it again. And it works!

Hope this helps someone :)

0
source share

For Swift 3

The method has changed to:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

from

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)

Note the "_" and the overlay of "locations" on [CLLocation], not on [AnyObject]!

If you use the old method, it will never be called, and you will not receive a warning about its change.

0
source share

All Articles