CLLocationManager startUpdatingLocation not workin

2019-01-26 08:32发布

问题:

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.

回答1:

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.



回答2:

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];


回答3:

I think, You can make this work by two ways:

  1. Using CLLocation framework

Check that, you have adopted the ViEWController with 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 framework You can do this by using the MKMapViewDelegate method named didUpdateUserLocation: Here you don't need the CLLocaionManager, This will be done by: In ViewController.h:

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

and In 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


回答4:

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."



回答5:

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.



回答6:

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



回答7:

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.