CesiumJS
- CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
-
CesiumJS 官网:https://www.cesium.com/
-
CesiumJS 下载地址:https://www.cesium.com/platform/cesiumjs/
-
CesiumJS API 文档:https://cesium.com/learn/cesiumjs/ref-doc/index.html
一、监听鼠标滚轮
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ScreenSpaceEventHandler - 监听鼠标滚轮</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction((delta) => {
console.log("滚动量为:", delta);
}, Cesium.ScreenSpaceEventType.WHEEL);
</script>
</html>
二、监听鼠标左键按下与松开
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ScreenSpaceEventHandler - 监听鼠标左键按下与松开</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction((click) => {
console.log("鼠标左键按下了!!!位置为:", click.position.x, click.position.y);
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
handler.setInputAction((click) => {
console.log("鼠标左键松开了,位置为:", click.position.x, click.position.y);
}, Cesium.ScreenSpaceEventType.LEFT_UP);
</script>
</html>
三、监听鼠标右键按下与松开
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ScreenSpaceEventHandler - 监听鼠标右键按下与松开</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction((click) => {
console.log("鼠标右键按下了!!!位置为:", click.position.x, click.position.y);
}, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
handler.setInputAction((click) => {
console.log("鼠标右键松开了,位置为:", click.position.x, click.position.y);
}, Cesium.ScreenSpaceEventType.RIGHT_UP);
</script>
</html>
四、监听鼠标左击落点
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ScreenSpaceEventHandler - 监听鼠标左击落点</title>
<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
<script>
const viewer = new Cesium.Viewer("container");
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (click) {
// 获取左击位置的射线
const pickRay = viewer.camera.getPickRay(click.position);
// 在地球表面找到与射线相交的点
const pickPosition = viewer.scene.globe.pick(pickRay, viewer.scene);
if (Cesium.defined(pickPosition)) {
// 如果找到了交点,将其转换为地理坐标(Cartographic)
const cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(pickPosition);
// 这里转换成直观的地理度数坐标
console.log("左击落点在地球球面,地理坐标为:", Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude));
} else {
console.log("左击落点不在地球球面");
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
</script>
</html>