Get Latitude Longitude and Height on terrain by left click in Cesium
General
JavaScript
CesiumJS
- adds a terrain
- picks position info by clicking on terrain
adds a terrain
picks position info by clicking on terrain
Description :
Simple Expression :
var promise = Cesium.sampleTerrainMostDetailed(viewer.terrainProvider, [carto]);
Cesium.when(promise, function(updatedPositions) {
console.log("Lon " + lon + "\nLat" + lat + "\nHeight " + updatedPositions[0].height);
});
Example :
// Grant CesiumJS access to your ion assets
Cesium.Ion.defaultAccessToken = "XXX";
try {
const viewer = new Cesium.Viewer("cesiumContainer", {
terrainProvider:
await Cesium.CesiumTerrainProvider.fromIonAssetId(1),
});
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
handler.setInputAction(async function(event) {
var position = viewer.scene.pickPosition(event.position);
if (Cesium.defined(position)) {
var carto = Cesium.Cartographic.fromCartesian(position);
var lat = Cesium.Math.toDegrees(carto.latitude);
var lon = Cesium.Math.toDegrees(carto.longitude);
console.log("Getting sample...");
try {
// Using async/await to handle the promise returned by sampleTerrainMostDetailed
var updatedPositions = await Cesium.sampleTerrainMostDetailed(viewer.terrainProvider, [carto]);
console.log("Lon: " + lon + "\nLat: " + lat + "\nHeight: " + updatedPositions[0].height);
} catch (error) {
console.error("Error sampling terrain: ", error);
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
} catch (error) {
console.log(error);
}
2019 Dec