cesium教程-3(显示高度,海拔,经度,纬度)

老规矩先看效果

在这里插入图片描述
解析:
在Cesium里面,我们可以通过Cesium.ScreenSpaceEventHandler的实例化对象的setInputAction方法绑定鼠标事件:

var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
//Cesium.ScreenSpaceEventType.MOUSE_MOVE :监听鼠标移动
handler.setInputAction(function(event) {

}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

//其他鼠标事件监听
//Cesium.ScreenSpaceEventType.LEFT_CLICK //鼠标左键单击事件
//Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK //鼠标左键双击事件
//Cesium.ScreenSpaceEventType.LEFT_DOWN //鼠标左键按下事件
//Cesium.ScreenSpaceEventType.LEFT_UP //鼠标左键抬起事件
//Cesium.ScreenSpaceEventType.MIDDLE_CLICK //鼠标中键单击事​​件
//Cesium.ScreenSpaceEventType.MIDDLE_DOWN //鼠标中键按下事件
//Cesium.ScreenSpaceEventType.MIDDLE_UP //鼠标中键抬起事件
//Cesium.ScreenSpaceEventType.MOUSE_MOVE //鼠标移动事件
//Cesium.ScreenSpaceEventType.PINCH_END //触摸表面上的双指事件的结束
//Cesium.ScreenSpaceEventType.PINCH_MOVE //触摸表面上双指移动事件
//Cesium.ScreenSpaceEventType.PINCH_START //触摸表面上双指事件的开始
//Cesium.ScreenSpaceEventType.RIGHT_CLICK //鼠标右键单击事件
//Cesium.ScreenSpaceEventType.RIGHT_DOWN //鼠标右键按下事件
//Cesium.ScreenSpaceEventType.WHEEL //鼠标滚轮事件
//


handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
//解除鼠标左键单击事件

实现显示高度,海拔,经纬度,的方法代码

    show3DCoordinates() {
        //地图底部工具栏显示地图坐标信息
       
        var coordinatesDiv = document.getElementById("map_coordinates");
        if (coordinatesDiv) {
            coordinatesDiv.style.display = "block";
        } else {
            coordinatesDiv = document.createElement("div");
            coordinatesDiv.id = "map_coordinates";
            coordinatesDiv.style.zIndex = "50";
            coordinatesDiv.style.bottom = "1px";
            coordinatesDiv.style.height = "29px";
            coordinatesDiv.style.position = "absolute";
            coordinatesDiv.style.overflow = "hidden";
            coordinatesDiv.style.textAlign = "center";
            coordinatesDiv.style.padding = '0 10px'
            coordinatesDiv.style.background = "rgba(0,0,0,0.5)"
            coordinatesDiv.style.left = "0";
            coordinatesDiv.style.bottom = "0"
            coordinatesDiv.style.lineHeight = "29px";
            coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>暂无坐标信息</span>";
            document.getElementById(this.id).append(coordinatesDiv);//this.id为球的根节点
            var handler3D = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
            handler3D.setInputAction((movement) => {
                var pick = new Cesium.Cartesian2(movement.endPosition.x, movement.endPosition.y);
                if (pick) {
                    var cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(pick), this.viewer.scene);
                    if (cartesian) {
                        //世界坐标转地理坐标(弧度)
                        var cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
                        if (cartographic) {
                            //海拔
                            var height = this.viewer.scene.globe.getHeight(cartographic);
                            //视角海拔高度
                            var he = Math.sqrt(this.viewer.scene.camera.positionWC.x * this.viewer.scene.camera.positionWC.x + this.viewer.scene.camera.positionWC.y * this.viewer.scene.camera.positionWC.y + this.viewer.scene.camera.positionWC.z * this.viewer.scene.camera.positionWC.z);
                            var he2 = Math.sqrt(cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z);
                            //地理坐标(弧度)转经纬度坐标
                            var point = [cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180];
                            if (!height) {
                                height = 0;
                            }
                            if (!he) {
                                he = 0;
                            }
                            if (!he2) {
                                he2 = 0;
                            }
                            if (!point) {
                                point = [0, 0];
                            }
                            coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>视角高度:" + (he - he2).toFixed(2) + "米&nbsp;&nbsp;&nbsp;&nbsp;海拔高度:" + height.toFixed(2) + "米&nbsp;&nbsp;&nbsp;&nbsp;经度:" + point[0].toFixed(6) + "&nbsp;&nbsp;纬度:" + point[1].toFixed(6) + "</span>";
                        }
                    }
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        }
    }

代码放在:https://github.com/weshmily/cesiumPDG

欢迎各位大佬点星星

作者

作者: weshmily前端
官网: 百度搜索(weshmily前端)
CSDN博客:http://blog.csdn.net/qq_27118895
GitHub: https://github.com/weshmily
公众号:搜索"weshmilyqd"
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小孟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值