Query Elevation (lines)

This sample demonstrates how to combine an Elevation Query on the ground with route. It queries the elevation along a route specified by the user and uses this data to extract statistics about the route, including total ascent and descent along the path.

How it works

The routing service requires a token for authentication. This sample uses an API Key to authenticate. You can either replace it with your own API Key, or remove it and log in once prompted. Alternatively, you can use another authentication method to access the routing service.

For details about the routing functionality please see the Route example, which serves as a basis for this example. We use the output from the route to do an elevation query on the ground surface of our map:

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
const route = data.routeResults[0].route;
const geometry = route.geometry;

const elevationPromise = map.ground.queryElevation(
  new Polyline({
    paths: geometry.paths,
    spatialReference: geometry.spatialReference
  })
);

This call returns a promise which resolves to a ElevationQueryResult object containing a new geometry with updated elevation data. We extract the first path from it and iterate over its stops, each of them represented as a [x,y,z] coordinate triplet.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
elevationPromise.then((result) => {
  const path = result.geometry.paths[0];
  let ascent = 0;
  let descent = 0;

  for (let i = 1; i < path.length; i++) {
    const d = path[i][2] - path[i - 1][2];
    if (d > 0) {
      ascent += d;
    } else {
      descent -= d;
    }
  }

  // update the text fields, etc ...
});

For our use case we are interested in the difference in elevation (stored in the z coordinate) between consecutive stops in the path. We sum up the respective deltas, and display them as total ascent and descent to the user.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.