大家好,我需要帮助,我正在扑扑中跟踪应用程序,想要计算总高程增益。我从位置拉出高度,但无法在旅途中计算总高程增益,因此我将所有海拔值保存在列表中。基本上需要一个可以在旅途中提供总高程增益的公式。 在颤动中像这样拉高
海拔= location.coords.altitude.toString();
答案 0 :(得分:0)
请阅读以了解代码
我们正在比较三个高程点:
让我们以用户开始时的海拔高度为100m为例
三个标高,例如:700m,1000m,800。 以上三个高程表明用户到达1000m后进行了下降。因此对于高程增益为1000-100m = 900m,这是真实的高程增益。减去的100m是行程开始时记录的海拔高度。
现在让我们考虑下三个高程为800m,600m,700m,这表明用户下降到600m后再次开始爬升。
所以现在真正的下降将是计算用户从1000m下降了多少,因此真正的下降将是1000m-600m = 400m
// This jut stimulates elevations in a small trip
// instead of this list call the functions whenever you record a elevation.
// and on the end of the trip call the [onStopTrip] function.
List<double> listOfDemoElevations = [
200.0, 300.0,400.0,1000.0,800.0,700.0,600.0,500.0,600.0,800.0,300.0,200.0];
///This Code Snippet compares Elevation of three points to see if the person has started ascending or is descending.
/// Then the code only stores the point after which user asecded or descended.
/// Then Calulates the total Elevation Gain or Elevation Loss.
double elevationGain = 0.0;
double elevationLoss = 0.0;
double startTripElevation = 100.0; //Record the StartTripElevation;
double previousElevation = startTripElevation;
double beforePreviousElevation = startTripElevation;
double adjustment =startTripElevation ; // this to get the truw ascend or descend
// This function check the three elevation then adds to elevation gain or loss accordinly with adjustment so true elevation gain or loss is recorded.
void calculateElevationGainLoss(double currentElevation) {
if (beforePreviousElevation < previousElevation &&
previousElevation > currentElevation) {
elevationGain = elevationGain + (previousElevation - adjustment);
adjustment = previousElevation;
} else if (beforePreviousElevation > previousElevation &&
currentElevation > previousElevation) {
elevationLoss = elevationLoss + (adjustment - previousElevation);
adjustment = previousElevation;
}
beforePreviousElevation = previousElevation;
previousElevation = currentElevation;
}
/// this function check if the elevation at which the user ended the trip to be added to elevation gain or loss
void onStopTrip(double currentElevation) {
if (adjustment > currentElevation) {
elevationLoss = elevationLoss + (adjustment - currentElevation);
} else if (adjustment < currentElevation) {
elevationGain = elevationGain + (currentElevation - adjustment);
}
}
listOfDemoElevations.forEach((element) {
calculateElevationGainLoss(element);
});
// Call this function when user hits end trip
onStopTrip(listOfDemoElevations.last);
print('Elevation gain=$elevationGain');
print('Elevation loss=$elevationLoss');
您可以将整个代码放在dartpad中的void main{}
中并进行测试: