CLLocation 调用委托方法
CLLocation calling delegate methods
我试图在切换到特定视图控制器 (FeedVC) 时获取用户位置,这是我目前拥有的代码:
FeedVC.h
#import <CoreLocation/CoreLocation.h>
@interface FeedVC : UIViewController <CLLocationManagerDelegate>
FeedVC.m
@implementation FeedVC
{
CLLocationManager *locationManager;
CLLocation *location;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (!locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"did update locations");
location = [locations lastObject];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
-(void) viewWillAppear:(BOOL)animated
{
NSLog(@"%.2f", location.coordinate.latitude);
}
方法 locationManager: didUpdateLocations:
没有被调用(因为 NSLog 没有显示)我已经按照这里的所有文档和其他答案,但我找不到问题的根源,谢谢!
不要在viewDidLoad中调用[locationManager startUpdatingLocation];
,
在委托方法中调用它:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
}
}
如果您还没有在您的 plist 中添加 NSLocationWhenInUseUsageDescription 添加它来描述您想要使用定位服务的原因
我试图在切换到特定视图控制器 (FeedVC) 时获取用户位置,这是我目前拥有的代码:
FeedVC.h
#import <CoreLocation/CoreLocation.h>
@interface FeedVC : UIViewController <CLLocationManagerDelegate>
FeedVC.m
@implementation FeedVC
{
CLLocationManager *locationManager;
CLLocation *location;
}
- (void)viewDidLoad {
[super viewDidLoad];
if (!locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"did update locations");
location = [locations lastObject];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
-(void) viewWillAppear:(BOOL)animated
{
NSLog(@"%.2f", location.coordinate.latitude);
}
方法 locationManager: didUpdateLocations:
没有被调用(因为 NSLog 没有显示)我已经按照这里的所有文档和其他答案,但我找不到问题的根源,谢谢!
不要在viewDidLoad中调用[locationManager startUpdatingLocation];
,
在委托方法中调用它:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[locationManager startUpdatingLocation];
}
}
如果您还没有在您的 plist 中添加 NSLocationWhenInUseUsageDescription 添加它来描述您想要使用定位服务的原因