Xcode 6 为啥 CLLocationManager 没有返回纬度和经度
Posted
技术标签:
【中文标题】Xcode 6 为啥 CLLocationManager 没有返回纬度和经度【英文标题】:Xcode 6 why CLLocationManager didn't return latitude and longitudeXcode 6 为什么 CLLocationManager 没有返回纬度和经度 【发布时间】:2015-03-14 09:41:50 【问题描述】:为什么我能得到方向,却不能得到经纬度? 我需要获取用户的位置信息准入并写入我的代码吗?
这是我的代码
#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.
【问题讨论】:
@DipenChudasama 如何接受 只需按“右”刻度标记..它以灰色显示在答案下方。 【参考方案1】:配置后,位置管理器必须“启动” 对于 ios 8,需要特定的用户级别权限, “使用时”授权授予对用户位置的访问权限 重要:一定要包括 NSLocationWhenInUseUsageDescription 及其 Info.plist 或 startUpdatingLocation 中的解释字符串将不起作用。
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[self.locationManager requestWhenInUseAuthorization];
【讨论】:
【参考方案2】:你必须替换下面的代码。
使用newLocation
而不是manager
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
self.Lat.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSLog(@"%f",newLocation.coordinate.latitude);
self.Long.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
【讨论】:
以上是关于Xcode 6 为啥 CLLocationManager 没有返回纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章
为啥我们不能在 xcode 6.1.1 中为推送通知选择配置文件和代码签名
为啥我的弹出框在 iPhone 6 Plus 上的 iOS 8.1 中崩溃? iOS 8 工作(实际上是 Xcode 6.0 到 6.1 的错误)
当我尝试在 Xcode 6.1 中调整情节提要中其他视图的大小时,为啥某些视图的框架变为 (0,0,0,0)
为啥我在 xcode 6 beta 中得到“必须将自动调整大小掩码转换为约束才能拥有 _setHostsLayoutEngine:YES”