I think what you’ll want is in the Picking Sandcastle example.
Click “Pick Position”, and the example code starts at line #127:
name : ‘milktruck’,
position : Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),
model : {
uri : ‘…/…/…/…/Apps/SampleData/models/CesiumMilkTruck/CesiumMilkTruck-kmc.gltf’
}
});
viewer.zoomTo(modelEntity);
var labelEntity = viewer.entities.add({
label : {
show : false,
showBackground : true,
font : ‘14px monospace’,
horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
verticalOrigin : Cesium.VerticalOrigin.TOP,
pixelOffset : new Cesium.Cartesian2(15, 0)
}
});
// Mouse over the globe to see the cartographic position
handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler.setInputAction(function(movement) {
var foundPosition = false;
var scene = viewer.scene;
if (scene.mode !== Cesium.SceneMode.MORPHING) {
var pickedObject = scene.pick(movement.endPosition);
if (scene.pickPositionSupported && Cesium.defined(pickedObject) && pickedObject.id === modelEntity) {
var cartesian = viewer.scene.pickPosition(movement.endPosition);
if (Cesium.defined(cartesian)) {
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(2);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(2);
var heightString = cartographic.height.toFixed(2);
labelEntity.position = cartesian;
labelEntity.label.show = true;
labelEntity.label.text =
‘Lon: ’ + (’ ’ + longitudeString).slice(-7) + ‘\u00B0’ +
‘\nLat: ’ + (’ ’ + latitudeString).slice(-7) + ‘\u00B0’ +
‘\nAlt: ’ + (’ ’ + heightString).slice(-7) + ‘m’;
labelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.0, -cartographic.height * (scene.mode === Cesium.SceneMode.SCENE2D ? 1.5 : 1.0));
foundPosition = true;
}
}
}
if (!foundPosition) {
labelEntity.label.show = false;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
``