CLLocationManager startUpdatingLocation not workin

2019-01-26 08:50发布

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.

7条回答
smile是对你的礼貌
2楼-- · 2019-01-26 09:09

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];
查看更多
beautiful°
3楼-- · 2019-01-26 09:09

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-26 09:18

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 :)

查看更多
在下西门庆
5楼-- · 2019-01-26 09:27

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.

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

"Implementation of this method is optional. You should implement this method, however."

查看更多
爷的心禁止访问
6楼-- · 2019-01-26 09:29

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.

查看更多
等我变得足够好
7楼-- · 2019-01-26 09:31

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.

查看更多
登录 后发表回答