sampleTerrainMostDetailed help with terrain profile

1. A concise explanation of the problem you’re experiencing.

I am attempting to sample terrain heights at specific positions. I am currently testing this feature with some coordinates around Mount Everest and NYC and I am receiving only zeros.

2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.

var terrainProvider = Cesium.createWorldTerrain();

var positions = [

Cesium.Cartographic.fromDegrees(86.925145, 27.988257),

Cesium.Cartographic.fromDegrees(87.0, 28.0)

];

var promise = Cesium.sampleTerrainMostDetailed(terrainProvider, positions);

Cesium.when(promise, function(updatedPositions) {

// positions[0].height and positions[1].height have been updated.

// updatedPositions is just a reference to positions.

});

console.log(positions[0].height);

console.log(positions[1].height);

3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

I am wanting to create a terrain profile graph based off of coordinates or potential a user drawn poly-line.

Similar to these examples: https://www.bluemarblegeo.com/knowledgebase/global-mapper-19/Path_Profile/PathProfile_3DCutaway.htm

4. The Cesium version you’re using, your operating system and browser.

Newest Cesium, Google Chrome

I think the problem there is just that you’re putting your console.log outside of the promise. Since sampleTerrainMostDetailed is asynchronous, it takes a few frames to get the result, so you’re checking the result before it finishes.

Try moving your console.log inside the promise callback. Let me know if that works!

hi, I meet the same problem as above, and my console.log inside the promise callback, but I am receiving only zero too. What may be causing this, hope to get your insights.

Should also add that there’s a nifty utility function to calculate points in a 3D line between two points (via a factor), so you can do something like;

let startPoint = Cartesian3(...);
let endPoint = Cartesian3(...);
const numberOfPoints = 50;

let points = [];
for ( let i = 1; i < numberOfPoints; i++ ) {
   let factor = i / numberOfPoints;
   points.push ( Cesium.Cartesian3.lerp ( startPoint, endPoint, factor, new Cesium.Cartesian3() ) );
}
console.log ( 'My points', points );

Happy hunting!

Cheers,

Alex