I have application that track user movement. And I store all relevant data lat/lng/alt etc.
I am trying add elevation like on runkeeper just without graphic I need just
According to the Wikipedia article you posted, "cumulative elevation gain" is basically the sum of all increases in elevation.
So for example, say you hike 100 feet up, then 100 feet down, then 200 feet up, then 250 feet down (say, a valley), and then 100 feet up, your gain would be 100 + 200 + 150 = 450 feet. The last 150 is due to hiking to an elevation of -50 feet at some point, then 100 feet up again.
Now, what this means to you is that you simply need to take into account positive deltas of altitude, like so:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
double elevationChange = oldLocation.altitude - newLocation.altitude;
// Only take into account positive changes
if (elevationChange > 0)
{
netElevationGain += elevationChange;
}
}
You could even simplify it further:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
netElevationGain += MAX(0, oldLocation.altitude - newLocation.altitude);
}
This would take into account valleys and even "ups and downs" during ascent and descent (which should be counted according to the article).
At the end, the netElevationGain
property will contain your gain.