2014-10-07 30 views
0

我已經實現了下面的代碼,通過使用CLLocationDistance方法添加(積累)所有點對點距離來計算gps使用中的總距離。我創建了一個計數器(_locManagerIteration),在第一次迭代中,我將第一個locationManager對象複製到了我的pastCoordinate屬性。在每次下一次迭代時,我使用CLLocationDistance方法來計算pastCoordinate對象和新loc對象之間的點到點距離。在程序運行時,我發現_pointDistance變量(double)正在收集距離,但總距離(_totalDistance)變量(也是double)始終與_pointDistance變量相同(即沒有距離累加)。出於某種不可解釋的原因,_totalDistance = _totalDistance + _pointDistance語句不是執行,或者_totalDistance變量始終爲零。下面的NSLog語句顯示了同樣的結果...你能幫忙嗎?CLLocationDistance方法不積累距離

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    _locManagerIteration++; 

    CLLocation* loc = [locations lastObject]; 

    if (_locManagerIteration == 1) { 
     self.pastCoordinate = [locations lastObject]; 
    } else { 

     CLLocationDistance _pointDistance = [self.pastCoordinate distanceFromLocation:loc]; 
     NSLog(@"POINT TO POINT DISTANCE = %f", _pointDistance); 
     _totalDistance = _totalDistance + _pointDistance; 
     self.pastCoordinate = [locations lastObject]; 
    } 

    NSLog(@"TOTAL DISTANCE = %f", _totalDistance); 

回答

0

你觀察到了多少次迭代?做這個實驗時你真的很努力嗎?您正在使用的模式中的GPS可能確實不準確,特別是在封閉空間中。然後,新的位置可能總是以相同的位置出現,並且您最終會添加零。

+0

我採取了一些迭代(超過10)。點距離(根據CLLocationDistance方法計算)在3到10(米)之間變化。實驗在內部進行,但當我在汽車中使用該應用程序時,結果是相同的...我完全處於虧損狀態,也許在那裏是XCODE中的一個錯誤?在CLLocationDistance調用時,_poinDistance變量的顏色是黑色(非伊娃),當我通過NSLog打印它時,變量coror顯示爲綠色(對於伊娃變量是正確的)。 – DaliGR 2014-10-08 18:49:55