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>
相关推荐
Monly214 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
boligongzhu5 分钟前
DALSA工业相机SDK二次开发(图像采集及保存)C#版
开发语言·c#·dalsa
Eric.Lee20215 分钟前
moviepy将图片序列制作成视频并加载字幕 - python 实现
开发语言·python·音视频·moviepy·字幕视频合成·图像制作为视频
7yewh7 分钟前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
长风清留扬9 分钟前
小程序毕业设计-音乐播放器+源码(可播放)下载即用
javascript·小程序·毕业设计·课程设计·毕设·音乐播放器
waicsdn_haha19 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
_WndProc21 分钟前
C++ 日志输出
开发语言·c++·算法
web1478621072323 分钟前
C# .Net Web 路由相关配置
前端·c#·.net
m0_7482478024 分钟前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
飞的肖27 分钟前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构