2012-11-10 106 views
-2
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 

    if (startingPoint == nil) 
     self.startingPoint = newLocation 


    CLLocationDistance distance = [newLocation 
           getDistanceFrom:startingPoint]; 



    NSString *distanceString = [[NSString alloc] 
           initWithFormat:@"%.2f Miles", totalDistance *0.000621371]; 
    DistanceTotal.text = distanceString; 
} 




@end 

我正在爲我的應用程序製作距離計算器。當我運行應用程序時,沒有距離信息的文本框是空白的。你能幫我找到問題嗎? (文本字段被稱爲DistanceTotal)謝謝:)位置管理器的換位距離

+0

犯錯,如果你開始回到起點,那麼你減少到那個位置的距離?我錯過了什麼嗎? – trojanfoe

+0

@casperOne我已經編輯我的問題,以滿足堆棧溢出問題的要求... –

回答

2

你試圖計算累計距離?當你接近開始時,從開始到當前位置的距離應該減小。

也許你想這個....

// in the class containing the location manager 
@property (nonatomic, assign) CLLocationDistance totalDistance; 

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation { 

    //if (startingPoint == nil) 
    // self.startingPoint = newLocation 


    //CLLocationDistance distance = [newLocation 
    //        getDistanceFrom:startingPoint]; 

    if (!oldLocation) 
     totalDistance == 0.0; 
    else 
     totalDistance += [newLocation distanceFromLocation:oldLocation];   

    NSString *distanceString = [[NSString alloc] 
           initWithFormat:@"%.2f Miles", totalDistance *0.000621371]; 
    distanceTraveledLabel.text = distanceString; 
} 
+2

這部分是正確的..但它不一遍又一遍添加三角洲但整體DIST ... –

+0

我應該在哪裏把這個代碼 –

+1

好點@ Daij-Djan。將編輯並說明放置的位置。 – danh

-1

手動從的LocationManager逢回調:)

fullDistance = 0 ;//add it up 
newDistance = GPS();//get with location 
oldDistance 

delta = abs(oldDist - newDist) 
full += delta; 

old = new