2011-02-02 144 views
10

此前的iOS 4.0,CoreLocation正确报告的高度,现在,它始终为0英尺报告。的iOS CoreLocation海拔

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSString *tLatitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude]; 
    NSString *tLongitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude]; 
    /* the following returns 0 */ 
    NSString *tAltitude = [NSString stringWithFormat:@"%i", newLocation.altitude]; 

    /* theres more code, but it's not relevant, 
         and this worked prior to iOS 4.0*/ 
    [manager stopUpdatingLocation]; 
} 

不工作的设备,也没有模拟器,没有任何人遇到此问题?

+0

您是在设备上还是在模拟器上测试此功能? – 2011-02-02 04:56:14

+0

设备和模拟器。 – WrightsCS 2011-02-02 05:00:10

回答

11

如果您使用startMonitoringSignificantLocationChanges,这是iOS 4.0的新功能,那么您将无法获得高度更新。这种低功耗模式仅使用蜂窝塔来确定用户的位置,而此方法不报告海拔高度。更一般地说,iPhone有三种方式来确定你的位置 - 手机信号塔,Wi-Fi和GPS。 GPS使用时您只能获得高度。因此,即使您将您的desiredAccuracy设置设置为非常精确以强制设备使用GPS,但如果用户在室内,iPhone可能无法获取GPS信号,并会回退到蜂窝或Wi-Fi。在这种情况下,你不会得到一个高度。还要考虑使用iPod Touch的用户 - 它只能通过Wi-Fi获取位置,因此也不会报告高度。

1

Core Location Data Types Reference

CLLocationDistance数据类型(海拔数据类型)是一个double。您用于stringWithFormat的格式化程序是integer。首先需要将高度投射到integer,或者使用double(%f)格式化器。

+0

我将高度转换为整数。正如我所说的,这在iOS 4.0之前完美运行(即无需更改代码)。 – WrightsCS 2011-02-02 03:36:08

1

你可以尝试两件事。首先,尝试将地点经理的desiredAccuracy设置为kCLLocationAccuracyBest

如果这样做不起作用,请尝试删除[manager stopUpdatingLocation];,并将NSLog放入didUpdateToLocation的高度。有时需要在显示高度之前缩小一点。

0

核心位置并不总是立即报告高度。我通常在“成功”委托方法中检查零高度。如果海拔高度仍然为0,我会继续检查,否则我将转向CoreLocation。