首页 / 浏览问题 / 三维GIS / 问题详情
webgl中无法注销鼠标事件
68EXP 2020年07月27日
我现在做的是当我选中checkbox的时候会显示状态条信息,没有选中的时候就把状态条信息,现在是能够显示,当我取消选择的时候那个状态条就没了,但是当我把鼠标再次移动到地图上的时候状态条还是会显示出来的。代码中我在没选中状态下是将这个鼠标事件注销了,但不知道为什么就是不好用。

            if(item.id == 'zhuangtaitiao') {
                var oMouseInfo = document.getElementsByClassName('mouse-pos-info');
                if(event.target.checked) {
                    console.log("显示状态条");
                    console.log(event);
                    var MouseHandler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
                    MouseHandler.setInputAction(function(movement) {
                            var cartesian = viewer.scene.pickPosition(movement.endPosition);
                            if(cartesian) {
                                var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                                var height = cartographic.height;
                                var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(4);
                                var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(4);
                                var sLongitude = longitudeString > 0 ? longitudeString + " E" : longitudeString.substring(1) + " W";
                                var sLatitude = latitudeString > 0 ? latitudeString + " N" : latitudeString.substring(1) + " S";
                                var sAltitude = "高度:" + Math.abs(height.toFixed(4)) + "米  ";

                                oMouseInfo[0].innerHTML = sAltitude + " " + sLatitude + " " + sLongitude;
                                oMouseInfo[0].setAttribute('style', 'display: block');

                            } else {
                                oMouseInfo[0].setAttribute('style', 'display: none');
                            }

                    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

                } else {
                    console.log("关闭状态条");
                            //MouseHandler.destroy();
                            //MouseHandler= MouseHandler && MouseHandler.destroy();
                            viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                            //flag=false;
                            //MouseHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                            oMouseInfo[0].setAttribute('style', 'display: none');
                            //oMouseInfo[0].removeAttribute('style','display');
                }
            }

1个回答

您好,您将 var MouseHandler = new Cesium.ScreenSpaceEventHandler(scene.canvas)这句移出来,写作全局变量。
1,545EXP 2020年07月27日
...