0

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 to get elevation value.
In my .h file:

@property (nonatomic) double netElevationLoss;
@property (nonatomic) double netElevationGain;
@property (nonatomic) double netElevationChange;

In my .m file:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    double elevationChange = oldLocation.altitude - newLocation.altitude;
    if (elevationChange < 0)
    {
        netElevationLoss += fabs(elevationChange);
    }
    else
    {
        netElevationGain += elevationChange;
    }

    netElevationChange = netElevationGain - netElevationLoss;
...

I don't know is this correct way to calculate it.
I have tested it and alt is for example 182.53 and netElevationChange is -182.53.
Maybe it's good but maybe I am missing something any idea what I have done wrong here?

1110
  • 7,829
  • 55
  • 176
  • 334
  • any help... did I provide too little information? – 1110 Oct 03 '13 at 12:22
  • Are you trying to map the elevation at each point or do you want to get the changes between each point? – Ian Oct 03 '13 at 12:32
  • I don't know I am just trying to get the same effect as in runkeeper app. Actually I don't understand difference between those two. Until today I didn't even know for elevation. – 1110 Oct 03 '13 at 12:43
  • If you want to store lat/lng and altitude as well, why don't just get it from `newLocation` and skip those calculations? – Ian Oct 03 '13 at 12:45
  • Wait you want to say that I don't need this calculations in didUpdateLocation? I already have stored lat/lng/altitude in my database. So what is that value (elevation) and how to get it to display it? – 1110 Oct 03 '13 at 12:51
  • According to [CLLocation](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/instp/CLLocation/altitude) documentation, it's "The altitude measured in meters" and "Positive values indicate altitudes above sea level. Negative values indicate altitudes below sea level.". – Ian Oct 03 '13 at 12:55
  • Are we talking about this http://en.wikipedia.org/wiki/Cumulative_elevation_gain – 1110 Oct 03 '13 at 12:58
  • Apparently not, didn't understand the concept. Let me have a look and I think I can provide an answer :) – Ian Oct 03 '13 at 13:03

1 Answers1

1

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.

Ian
  • 4,195
  • 2
  • 25
  • 32
  • Cool I understand now :) just need to test it. – 1110 Oct 03 '13 at 13:32
  • Yes it is. `altitude` is measured in meters. – Ian Oct 03 '13 at 14:10
  • Just a subquestion. As I store in database lat/lng/alt should I extend my table for one more field GAIN? And when I need to display data to the user to select just MAX(GAIN)? – 1110 Oct 04 '13 at 08:27
  • Well, gain would store the overall gain during the workout, so I don't think it would make much sense to store gain for each point, especially since you should be able to recalculate it from the altitude points. You could store the gain up to that point though (as a caching mechanism), up to you. – Ian Oct 04 '13 at 12:37
  • So you want to say that if someone drive bicycle for example and his MAX GAIN was 400 meters. I should care only about that 400 meters and save that for particular route, am I right? – 1110 Oct 04 '13 at 17:42
  • 1
    Right. Though, depending on your needs, you could store the current gain for each point (meaning you could know the gain up to that point). – Ian Oct 04 '13 at 18:53
  • I started a new question based on this one. As only you help me with this maybe you know what is the issue in resulting values. Question is http://stackoverflow.com/questions/19210248/calculating-cumulative-elevation-gain-give-me-wired-results – 1110 Oct 06 '13 at 15:21