Cesium equivalent of terrain.getHeight(x, y)

What is the equivalent of terrain.getHeight(x, y) in Cesium?

I found a few examples on non Cesium websites but they all seem to be outdated.
I am looking for a code snippet to perform something like that:

let height = (?someCesiumStructure?).getHeight(longitude, latitude);
let dog = viewer.entities.add({ //dog's 3d model
    position: Cesium.Cartesian3.fromDegrees(-121.732796, 36.4799, height),
    model: { uri: ionDogResource.url },
});
this.viewer.trackedEntity = dog;

So the below worked for me however I am not sure that is the right way (or most straight forward way) to do that?

Cesium.sampleTerrain(viewer.terrainProvider, 9, [pointOfInterest]).
    then((samples) => {
        console.log('Height in meters is: ' + samples[0].height);
    });
viewer.scene.globe.getHeight(Cesium.Cartographic.fromDegrees(longitude, latitude));

See this Sandcastle link

1 Like

The code below returns about -44000 for height?

this.viewer.scene.globe.depthTestAgainstTerrain = true;
const height = this.viewer.scene.globe.getHeight(Cesium.Cartographic.fromDegrees(longitude, latitude, 0));

your code relies on the zoom level,when you try to use this code to get right height you still can use my codes below

	const viewer = new Viewer('cesiumApp', {
			infoBox: false,
			terrain: Terrain.fromWorldTerrain(),
		});

		const clickHandler = new ScreenSpaceEventHandler(viewer.scene.canvas);

		clickHandler.setInputAction((event) => {

			const ray = viewer.camera.getPickRay(event.position);

			let cartesianCoordinates = viewer.scene.globe.pick(ray, viewer.scene);

			if (defined(cartesianCoordinates)) {
				let cartographic = Cartographic.fromCartesian(cartesianCoordinates);

				let latitudeString = CesiumMath.toDegrees(cartographic.latitude).toFixed(3);
				let longitudeString = CesiumMath.toDegrees(cartographic.longitude).toFixed(3);
				let heightString = cartographic.height.toFixed(2);
				let lhtext = `Lat: ${latitudeString.slice(-8)}, Lon: ${longitudeString.slice(
					-8
				)}, Alt: ${heightString.slice(-7)}`;

				console.log(lhtext);

				console.log(viewer.scene.globe.getHeight(cartographic));
			}
		}, ScreenSpaceEventType.LEFT_CLICK);
1 Like