Ios iPhone模拟器上的高度始终为零
我试图在Xcode和模拟器上开发一个高度计,但它总是返回0表示海平面以上的高度。 我不明白为什么,我在模拟器上试过很多地方 我的代码是:Ios iPhone模拟器上的高度始终为零,ios,cocoa-touch,gps,ios-simulator,cllocationmanager,Ios,Cocoa Touch,Gps,Ios Simulator,Cllocationmanager,我试图在Xcode和模拟器上开发一个高度计,但它总是返回0表示海平面以上的高度。 我不明白为什么,我在模拟器上试过很多地方 我的代码是: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _mapView.delegate = self; _locationManager = [[CLLocationManager alloc]
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapView.delegate = self;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.pausesLocationUpdatesAutomatically = YES;
_locationManager.delegate = self;
firstLocation = YES;
checkUserLocation = NO;
}
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
lastLocation = location.coordinate;
_latitudeLabel.text = [NSString stringWithFormat:@"Current latitude: %f", location.coordinate.latitude];
_longitudeLabel.text = [NSString stringWithFormat:@"Current longitude: %f", location.coordinate.longitude];
_altitudeLabel.text = [NSString stringWithFormat:@"Current altitude: %f m", location.altitude];
}
我的错误在哪里 您的代码没有问题,因为您使用的是模拟器 确定高度需要具有GPS功能的设备,并且您还需要在该设备上使用GPS才能获取高度(仅wifi位置即使在启用GPS的设备上也无法正确报告高度)。iOS模拟器没有这些功能,因此那里的高度将不准确。您需要使用带有GPS的真实设备来获取高度测量值 如果要使用高度模拟CLLocation,可以创建CLLocation对象并自己传递高度:
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
timestamp:(NSDate *)timestamp
高度是一个只读属性,因此当您在代理回调中收到CLLocation对象时,您需要自己创建一个新的CLLocation对象,而不是手动更改它