Xcode6 为什么CLLocationManager没有return经纬度

Xcode 6 why CLLocationManager didn't return latitude and longitude

为什么我能得到方向,却不能得到经纬度? 我需要获取用户的位置信息准入并写入我的代码吗?

这是我的代码

#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.
}

配置后,位置管理器必须 "started" 对于 iOS 8,需要特定用户级别的权限, "when-in-use" 授权授予对用户位置的访问权限 重要:确保包括 NSLocationWhenInUseUsageDescription 及其 Info.plist 或 startUpdatingLocation 中的说明字符串将不起作用。

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{
    [self.locationManager requestWhenInUseAuthorization];
}

你必须替换下面的代码。

使用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];

}