核心位置没有响应
Core Location not responding
当我输入以下代码并在模拟器上 运行 并将位置设置为 "City Run" 时,它不会记录任何内容。不过我不知道我做错了什么。
.h
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <UITextFieldDelegate,CLLocationManagerDelegate> {
CLLocationManager *locMgr;
}
@property (nonatomic, retain) CLLocationManager *locMgr;
@property NSInteger speed;
.m
@synthesize locMgr;
- (void)viewDidLoad {
[super viewDidLoad];
self.locMgr = [[CLLocationManager alloc] init];
self.locMgr.delegate = self;
self.locMgr.desiredAccuracy = kCLLocationAccuracyBest;
[self.locMgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.speed = roundf([newLocation speed]);
NSLog(@"speed: %ld", (long)self.speed);
}
你看到我遗漏了什么吗?
尝试将 NSLocationWhenInUseUsageDescription 键添加到您的 info.plist
您需要通过以下任一方法请求授权才能使用定位服务:
[self.locMgr requestAlwaysAuthorization];
或
[self.locMgr requestWhenInUseAuthorization];
当我输入以下代码并在模拟器上 运行 并将位置设置为 "City Run" 时,它不会记录任何内容。不过我不知道我做错了什么。
.h
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <UITextFieldDelegate,CLLocationManagerDelegate> {
CLLocationManager *locMgr;
}
@property (nonatomic, retain) CLLocationManager *locMgr;
@property NSInteger speed;
.m
@synthesize locMgr;
- (void)viewDidLoad {
[super viewDidLoad];
self.locMgr = [[CLLocationManager alloc] init];
self.locMgr.delegate = self;
self.locMgr.desiredAccuracy = kCLLocationAccuracyBest;
[self.locMgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.speed = roundf([newLocation speed]);
NSLog(@"speed: %ld", (long)self.speed);
}
你看到我遗漏了什么吗?
尝试将 NSLocationWhenInUseUsageDescription 键添加到您的 info.plist
您需要通过以下任一方法请求授权才能使用定位服务:
[self.locMgr requestAlwaysAuthorization];
或
[self.locMgr requestWhenInUseAuthorization];