Expose actualClampedPosition for Points
See original GitHub issueHere’s the scenario which came up on the forum:
You’re trying to draw a polyline from the air to the ground so it looks something like this:

You can’t use polylines clamped to ground because one of the points is in the air. One way to do it is to use sampleTerrainMostDetailed to get the actual terrain height and just use that as the other point.
// Plane position
var position = Cesium.Cartesian3.fromDegrees(-122.0744619, 44.0503706, 1600);
var groundPosition = new Cesium.Cartographic(-2.1305346621060743, 0.7688285707007808);
var promise = Cesium.sampleTerrainMostDetailed(viewer.terrainProvider, [groundPosition]);
Cesium.when(promise, function(updatedPositions) {
var finalGroundPos = Cesium.Cartographic.toCartesian(groundPosition);
viewer.entities.add({
polyline : {
positions : [position,finalGroundPos],
width : 10,
material : new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED)
}
});
});
But then this doesn’t work well if the point on the ground is moving. So to achieve something like this:

I created a point that was clamped to ground and used its position as the end point of the line. But that position doesn’t take into account the clamping to ground. So I had to dig it out of the private API:
var PointViz = viewer.dataSourceDisplay._defaultDataSource._visualizers[4];
var pos = new Cesium.BoundingSphere();
viewer.entities.add({
polyline : {
positions : new Cesium.CallbackProperty(function(time) {
PointViz.getBoundingSphere(groundPoint,pos);
return [position, pos.center];
//return [position, groundPoint.position.getValue(time)];
}, false),
width : 10,
material : new Cesium.PolylineArrowMaterialProperty(Cesium.Color.RED)
}
});
Here it is running in Sandcastle.
It seems like the Billboard at least does keep track of its actual clamped position:
https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Scene/Billboard.js#L916-L929
If it doesn’t degrade performance it would be nice to expose this for points (especially if it’s already computed every time the point updates). It would be a really easy way/fast way to get terrain height at any location in real time.
(I tried to look if there was an issue about this already but I couldn’t anything. If there’s already a way to accomplish this feel free to close).
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
Also, if you just want the height of the current visual terrain (so it always matches what the user sees on screen) scene.globe.getHeight() will give you that. Bonus that it’s synchronous so great for interactive editing. You’ll just want to do a single sampleTerrainMostDetailed call at the end though so that you have the “real” position. Documentation for
getHeightcould probably use some polish.@OmarShehata the best method for achieving something like this is to
position = viewer.scene.globe.pick(viewer.scene.camera.getPickRay(mousePosition))to get a rough position while the mouse is moving, then you can callsampleTerrainMostDetailedto get the final accurate position once the position is set.You never want to dig into the private API like that. There are usually ways to accomplish what you are trying to do using the public API.