cesium 跟踪鼠标移动 获取鼠标当前地理坐标信息

function show3DCoordinates(){
    //地图底部工具栏显示地图坐标信息
    var elementbottom = document.createElement("div");
    $(".cesium-viewer").append(elementbottom);
    elementbottom.style.width = "100%";
    elementbottom.style.height = "30px";
    elementbottom.style.background = "rgba(0,0,0,0.5)";
    elementbottom.style.position = "absolute";
    elementbottom.style.bottom = "0px";
    elementbottom.style.cursor = "default";

    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.left = "10px";
        coordinatesDiv.style.lineHeight = "29px";
        coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>暂无坐标信息</span>";
        $(".cesium-viewer").append(coordinatesDiv);
        var MouseHeight = 0;
        let handlers = new Cesium.ScreenSpaceEventHandler(viewer.scene._imageryLayerCollection);
        // 获取相机的海拔高度作为视角高度/km
        handlers.setInputAction(function (event) {
            //获取相机射线
            ray = viewer.scene.camera.getPickRay(event.endPosition);
            //根据射线和场景求出在球面中的笛卡尔坐标
            position1 = viewer.scene.globe.pick(ray,viewer.scene);
            //获取该浏览器坐标的顶部数据
            feature = viewer.scene.pick(event.endPosition);
            if (feature == undefined && position1) {
                cartographic1 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
                lon = Cesium.Math.toDegrees(cartographic1.longitude);
                lat = Cesium.Math.toDegrees(cartographic1.latitude);
                MouseHeight=0;
            }
            else{
                let cartesian = viewer.scene.pickPosition(event.endPosition);
                if (Cesium.defined(cartesian) ){
                    //如果对象已定义,将度转为经纬度
                    let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                    lon = Cesium.Math.toDegrees(cartographic.longitude);
                    lat = Cesium.Math.toDegrees(cartographic.latitude);
                    MouseHeight = cartographic.height;//模型高度
                }
            }
             // 获取相机的高度
            cameraHeight = Math.ceil(viewer.camera.positionCartographic.height);
            console.log('高度:',MouseHeight,'经度:',lon,'纬度:',lat,"视角:"+cameraHeight)
            coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>视角高度:"+cameraHeight+"米&nbsp;&nbsp;&nbsp;&nbsp;模型高度:"+MouseHeight.toFixed(2)+"米&nbsp;&nbsp;&nbsp;&nbsp;经度:" + lon.toFixed(6) + "&nbsp;&nbsp;纬度:" + lat.toFixed(6)+ "</span>";
        } , Cesium.ScreenSpaceEventType.MOUSE_MOVE);

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MichaelYZ111

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

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

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

打赏作者

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

抵扣说明:

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

余额充值