CLLocationManager startUpdatingLocation不起作用

所以现在我至less得到以下代码的callback…

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

我可以在第二个方法中设置断点,NSLog报告不断的位置更新,但由于某种原因,跨度缩放不起作用。 任何想法为什么? 这是我的坐标和一切。 在这一个上划伤我的头。

将CLLocationManager分配给类中的(强)属性。 (我假设你正在使用ARC BTW。)现在,CLLocationManager不会超过viewDidLoad方法的末尾,所以它也不会调用你的委托方法。

确保你已经在@interface文件中添加了<CLLocationManagerDelegate>

编辑

如果委托设置正确,请确保您使用的是locationManager属性:

.h文件中:

 @property (nonatomic, strong) CLLocationManager *locationManager; 

viewDidLoad

 self.locationManager = [[CLLocationManager alloc] init]; [self.locationManager setDelegate:self]; [self.locationManager startUpdatingLocation]; 

我想,你可以通过两种方式来完成这项工作:

  1. 使用CLLocation框架

检查一下,你是否已经采用了带有CLLocationManagerDelegate方法的ViEWController

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

在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.使用相同的MKMapKit框架你可以通过使用名为didUpdateUserLocation的MKMapViewDelegate方法来实现:在这里你不需要CLLocaionManager,这可以通过以下方式完成:在ViewController.h中:

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

并在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]; } -(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 

那么,首先,你永远不能确定位置pipe理者能够首先更新位置。 在更新过程中可能会出现错误,或者您无权访问用户的位置。

实现此CLLocationManager委托方法并validation错误。

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

“这个方法的实现是可选的,但是你应该实现这个方法。”

如果你在模拟器中运行它,只需要提示它改变坐标。 在Xcode中,在debugging输出窗格上方有一个典型的位置服务箭头。 接下来是位置的下拉列表。 一旦你的应用程序正在运行,切换它正在模拟的位置,看看这个改变是否触发你的代码。 然后在真实的设备上进行testing。

在我的情况下,它没有进入didChangeAuthorization。 我正在尝试一个真正的设备。

我从手机中删除了应用程序并重新安装。 它的工作原理!

希望这可以帮助别人:)

对于Swift 3

该方法已更改为:

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

从:

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

注意'_'和'位置'到[CLLocation]而不是[AnyObject]的转换!

如果您使用旧的方法,它将永远不会被调用,您将不会收到它已经更改的警告。