How to calculate elevation like in runkeeper application

Deadly 提交于 2019-12-01 13:04:41

问题


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?


回答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.



来源:https://stackoverflow.com/questions/19156415/how-to-calculate-elevation-like-in-runkeeper-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!