为什么我可以获得方向但我无法获得经度和纬度?我是否需要获得用户的位置信息许可并写入我的代码?
这是我的代码
#import "ViewController.h"
@interface ViewController ()<CLLocationManagerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingHeading];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
self.Lat.text = [NSString stringWithFormat:@"%f", manager.location.coordinate.latitude];
NSLog(@"%f",manager.location.coordinate.longitude);
self.Long.text = [NSString stringWithFormat:@"%f", manager.location.coordinate.longitude];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
self.Mag_heading.text = [NSString stringWithFormat:@"%f",newHeading.magneticHeading];
self.True_heading.text = [NSString stringWithFormat:@"%f", newHeading.trueHeading];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
配置完成后,必须针对iOS 8“启动”位置管理器,需要特定的用户级别权限,“使用时”授权将授予对用户重要位置的访问权限:请确保在您的应用中包括NSLocationWhenInUseUsageDescription以及其说明字符串Info.plist或startUpdatingLocation将不起作用。
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句