当前位置: 首页 > article >正文

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


一、监听鼠标滚轮

<!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>

二、监听鼠标左键按下与松开

<!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>

三、监听鼠标右键按下与松开

<!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>

四、监听鼠标左击落点

<!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>

http://www.kler.cn/a/379614.html

相关文章:

  • 打包IPA包上传,报错“您的可执行文件包含不受支持的体系结构(×86 _64 i386)”
  • rnn/lstm 项目实战
  • VisionPro —— CogPatInspectTool对比工具
  • 「C/C++」C/C++标准库 之 #include<cstdlib> 通用工具函数库
  • 防重方案-订单防重方案笔记
  • VirtualBox 解决虚拟机Cable Unplugged 无法上网问题
  • Linux:线程安全的单例模式
  • 进程的概念
  • Vue学习之路16----pinia
  • 家具产品的耐用性新标准,矫平机为家具制造提供新保障
  • SQL中`ORDER BY`、`SORT BY`、`DISTRIBUTE BY`、`GROUP BY`、`CLUSTER BY`的区别详解
  • 什么是严肃游戏,严肃游戏本地化的特点是什么?
  • 【C语言刷力扣】3216.交换后字典序最小的字符串
  • 第十五章 Vue工程化开发及Vue CLI脚手架
  • 贪心算法理论基础和习题【算法学习day.17】
  • Python代码解析:问题分类器实现
  • el-table type=“selection“换页多选数据丢失的解决办法
  • dify实战案例分享-基于多模态模型的发票识别
  • git submodule
  • 【AIGC】深入探索『后退一步』提示技巧:激发ChatGPT的智慧潜力
  • 【jvm】对象分配过程
  • PostgreSQL JOIN 操作深入解析
  • 《星光予你》系列网剧正式开机! “黑莲花”陷入时间循环攻略疯批霸总
  • 报错 sys_platform == “win32“ (from mmcv) (from versions: none)
  • excel表格文字识别-ocr表格文字提取api接口集成-python
  • 双向链表专题