2011-02-02 112 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。