I’m currently looking to get the terrain height on mouse click. Going through the documentation I found two methods to suit my use case. The first one is Globe.getHeight and the second one is Cesium.sampleTerrainMostDetailed.
What’s the difference between these two methods? The values returned by these two are different.
Test code:
Globe.getHeight
<Viewer
ref={ref}
onClick={(click) => {
if (click.position && ref.current?.cesiumElement) {
var ellipsoid = ref.current?.cesiumElement.scene.globe.ellipsoid;
var cartesian = ref.current?.cesiumElement.camera.pickEllipsoid(
click.position,
ellipsoid
);
if (cartesian) {
var cartographic = ellipsoid.cartesianToCartographic(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
var altitudeString =
ref.current?.cesiumElement.scene.globe.getHeight(
Cesium.Cartographic.fromDegrees(
cartographic.longitude,
cartographic.latitude
)
);
window.alert(
latitudeString + "," + longitudeString + "," + altitudeString
);
}
}
}}
/>
Cesium.sampleTerrainMostDetailed
<Viewer
ref={ref}
onClick={(click) => {
if (click.position && ref.current?.cesiumElement) {
var ellipsoid = ref.current?.cesiumElement.scene.globe.ellipsoid;
var cartesian = ref.current?.cesiumElement.camera.pickEllipsoid(
click.position,
ellipsoid
);
if (cartesian) {
var cartographic = ellipsoid.cartesianToCartographic(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
Cesium.sampleTerrainMostDetailed(Cesium.createWorldTerrain(), [
cartographic,
]).then(function (updatedPositions) {
window.alert(
longitudeString +
"," +
latitudeString +
"," +
updatedPositions[0].height
);
});
}
}
}}
/>