CesiumJS 案例 P20:监听鼠标滚轮、监听鼠标左键按下与松开、监听鼠标右键按下与松开、监听鼠标左击落点

CesiumJS

  • CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
  1. CesiumJS 官网:https://www.cesium.com/

  2. CesiumJS 下载地址:https://www.cesium.com/platform/cesiumjs/

  3. 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>
相关推荐
哟哟耶耶2 分钟前
css-设置元素的溢出行为为可见overflow: visible;
前端·css
sunly_5 分钟前
CSS:跑马灯
前端·css
小熊科研路(同名GZH)6 分钟前
【Matlab高端绘图SCI绘图模板】第05期 绘制高阶折线图
开发语言·matlab·信息可视化
&白帝&10 分钟前
JAVA JDK7时间相关类
java·开发语言·python
2301_8187320613 分钟前
用layui表单,前端页面的样式正常显示,但是表格内无数据显示(数据库连接和获取数据无问题)——已经解决
java·前端·javascript·前端框架·layui·intellij idea
geovindu13 分钟前
Qt Designer and Python: Build Your GUI
开发语言·qt
yqcoder14 分钟前
npm link 作用
前端·npm·node.js
Xiao Xiangζั͡ޓއއ15 分钟前
程序诗篇里的灵动笔触:指针绘就数据的梦幻蓝图<1>
c语言·开发语言·程序人生·学习方法·改行学it
林涧泣19 分钟前
【Uniapp-Vue3】页面和路由API-navigateTo及页面栈getCurrentPages
前端·vue.js·uni-app
狄加山67520 分钟前
系统编程(线程互斥)
java·开发语言