0
我試圖計算行駛的總距離並將其輸出到視圖控制器,但結果與預期不符。代碼如下:distanceFromLocation - 沒有正確計算總距離
MyCLController.m
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
validLocation = YES;
if (!newLocation)
{
validLocation = NO;
}
if (newLocation.horizontalAccuracy < 0)
{
validLocation = NO;
}
// Filter out points that are out of order
NSTimeInterval secondsSinceLastPoint = -[newLocation.timestamp timeIntervalSinceNow];
if (secondsSinceLastPoint < 0)
{
validLocation = NO;
}
if (validLocation == YES)
{
[self.delegate locationChange:newLocation :oldLocation];
}
NewWorkoutViewController.m
-(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation
{
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
currentSpeed = ([newLocation speed] * 3600)/1000;
totalDistance = (totalDistance + meters)/1000;
totalDistanceMeters = totalDistanceMeters + meters;
avgSpeed = totalDistance/counterInt;
[speedLbl1 setText:[NSString stringWithFormat:@"%.3f", currentSpeed]];
[distanceLbl1 setText:[NSString stringWithFormat:@"%.3f", totalDistance]];
}
問題是與我的totalDistance,它似乎並沒有被添加到它每一次,就像它覆蓋它,當我在車上測試時,我可以看到座標之間10/20米的值,所以這表明distanceFromLocation似乎正在工作。
任何任何想法?
的問候,斯蒂芬