iOS CoreLocation高度

10

iOS 4.0之前,CoreLocation报告的高度是正确的,现在它总是报告为0英尺。

-(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];
}

在设备和模拟器上都无法工作,有其他人遇到这个问题吗?


你是在设备上测试还是在模拟器上测试? - sudo rm -rf
4个回答

12

如果你正在使用iOS 4.0中新增的功能startMonitoringSignificantLocationChanges,那么你将无法获取高度更新。这种低功耗模式仅使用蜂窝塔来确定用户的位置,而这种方法不会报告高度。

更一般地说,iPhone有三种方式来确定你的位置 - 蜂窝塔、Wi-Fi和GPS。只有在使用GPS时才能获取高度信息。因此,即使你将desiredAccuracy设置为非常精确以强制设备使用GPS,如果用户在室内,iPhone可能无法获得GPS信号并会回退到蜂窝或Wi-Fi。在这种情况下,您将无法获得高度信息。还要考虑使用iPod Touch的用户-它只能通过Wi-Fi获得位置信息,因此也不会报告高度。


2

你可以尝试两件事情。首先,将位置管理器的desiredAccuracy设置为kCLLocationAccuracyBest

如果这样不行,请尝试删除[manager stopUpdatingLocation];,并在didUpdateToLocation中使用NSLog记录高度。有时候需要缩小范围一段时间才能显示高度。


kCLLocationAccuracyBest一直被设置,移除stopUpdatingLocation没有任何效果。 - WrightsCS

1

来自Core Location数据类型参考:

CLLocationDistance数据类型(高度的数据类型)是一个double。您正在使用的stringWithFormat格式化程序是integer。您需要首先将您的高度转换为integer或使用double(%f)格式化程序。


我正在将高度转换为整数。正如我所说,这在 iOS 4.0 之前完美地运行(即没有更改代码)。 - WrightsCS
此外,我所设置的方式使得高度完美显示,例如可以显示5280英尺。 - WrightsCS
@WrightsCS,您是否尝试了建议的双重格式化器,以查看它是否有所不同? - raidfive

0

Core Location 不总是立即报告海拔高度。通常我会在我的“成功”委托方法中检查零海拔高度。如果海拔高度仍然为0,我会继续检查,否则我会关闭 CoreLocation。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接