How to calculate elevation like in runkeeper application

后端 未结 1 1593
Happy的楠姐
Happy的楠姐 2021-01-16 20:03

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

1条回答
  •  时光取名叫无心
    2021-01-16 20:38

    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.

    0 讨论(0)
提交回复
热议问题