首页 / 浏览问题 / 三维GIS / 问题详情
如何给MeasureMode.Distance添加文字说明
nk
29EXP 2021年03月26日
handlerDis = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.Distance, 0);

    var tooltip = createTooltip(document.body);

    //注册测距功能事件

    handlerDis.measureEvt.addEventListener(function (result) {

        var dis = Number(result.distance);

        var positions = result.positions;

        // if (selOptV == 3 || selOptV == 4) {

        //     dis = Number(calcClampDistance(positions));

        // }

        var distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';

        handlerDis.disLabel.text = '距离:' + distance;

        handlerDis.disLabel.outlineColor = {}

    });

    handlerDis.activeEvt.addEventListener(function (isActive) {

        if (isActive == true) {

            viewer.enableCursorStyle = false;

            viewer._element.style.cursor = '';

            $('body').removeClass('measureCur').addClass('measureCur');

        }

        else {

            viewer.enableCursorStyle = true;

            $('body').removeClass('measureCur');

        }

    });

1个回答

添加文字说明是指鼠标光标旁边添加吗
6,087EXP 2021年03月26日
嗯是的,就是tooltip,但是我加了不知道为什么没有效果

    handlerDis.movingEvt.addEventListener(function (windowPosition) {

        if (handlerDis) {

            tooltip.showAt(windowPosition, '<p>再次单击左键结束绘制</p>');

        } else {

            tooltip.showAt(windowPosition, '<p>点击左键绘制第一个点</p>');

        }

    });
引入:    <script src="./js/tooltip.js"></script>

代码:

var tooltip = createTooltip(document.body);
            
            var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
        // 鼠标移动时间回调
        handler.setInputAction(function (e) {
            if (handlerDis) {
            tooltip.showAt(e.startPosition, '<p>再次单击左键结束绘制</p>');

        } else {
            tooltip.showAt(windowPosition, '<p>点击左键绘制第一个点</p>');
        }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
没效果啊

要这种效果吗

你看看是不是哪里冲突了?我这里还是不行

function initMeasure() {

    handlerDis = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.Distance, 0);

    var tooltip = createTooltip(document.body);

    //注册测距功能事件

    handlerDis.measureEvt.addEventListener(function (result) {

        var dis = Number(result.distance);

        var positions = result.positions;

        // if (selOptV == 3 || selOptV == 4) {

        //     dis = Number(calcClampDistance(positions));

        // }

        var distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';

        handlerDis.disLabel.text = '距离:' + distance;

        handlerDis.disLabel.outlineColor = {}

    });

    var tooltip = createTooltip(document.body);

    var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);

    handler.setInputAction(function (e) {

        if (handlerDis) {

            tooltip.showAt(e.startPosition, '<p>再次单击左键结束绘制</p>');

        } else {

            tooltip.showAt(windowPosition, '<p>点击左键绘制第一个点</p>');

        }

    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    handlerDis.activeEvt.addEventListener(function (isActive) {

        if (isActive == true) {

            viewer.enableCursorStyle = false;

            viewer._element.style.cursor = '';

            $('body').removeClass('measureCur').addClass('measureCur');

        }

        else {

            viewer.enableCursorStyle = true;

            $('body').removeClass('measureCur');

        }

    });

    handlerDis.movingEvt.addEventListener(function (windowPosition) {

        if (handlerDis.isDrawing) {

            tooltip.showAt(windowPosition, '<p>再次单击左键结束绘制</p>');

        } else {

            tooltip.showAt(windowPosition, '<p>点击左键绘制第一个点</p>');

        }

    });

    //初始化测量面积

    handlerArea = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.Area, 0);

    handlerArea.measureEvt.addEventListener(function (result) {

        var mj = Number(result.area);

        var positions = result.positions;

        // if (selOptV == 3 || selOptV == 4) {

        //     mj = Number(calcClampValue(positions));

        // } else if (selOptV == 5) {

        //     mj = Number(calcAreaWithoutHeight(positions));

        // }

        var area = mj > 1000000 ? (mj / 1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'

        handlerArea.areaLabel.text = '面积:' + area;

        handlerArea.areaLabel.outlineColor = {}

    });

    handlerArea.activeEvt.addEventListener(function (isActive) {

        if (isActive == true) {

            viewer.enableCursorStyle = false;

            viewer._element.style.cursor = '';

            $('body').removeClass('measureCur').addClass('measureCur');

        }

        else {

            viewer.enableCursorStyle = true;

            $('body').removeClass('measureCur');

        }

    });

    //初始化测量高度

    handlerHeight = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.DVH);

    handlerHeight.measureEvt.addEventListener(function (result) {

        var distance = result.distance > 1000 ? (result.distance / 1000).toFixed(2) + 'km' : result.distance + 'm';

        var vHeight = result.verticalHeight > 1000 ? (result.verticalHeight / 1000).toFixed(2) + 'km' : result.verticalHeight + 'm';

        var hDistance = result.horizontalDistance > 1000 ? (result.horizontalDistance / 1000).toFixed(2) + 'km' : result.horizontalDistance + 'm';

        handlerHeight.disLabel.text = '空间距离:' + distance;

        handlerHeight.vLabel.text = '垂直高度:' + vHeight;

        handlerHeight.hLabel.text = '水平距离:' + hDistance;

    });

    handlerHeight.activeEvt.addEventListener(function (isActive) {

        if (isActive == true) {

            viewer.enableCursorStyle = false;

            viewer._element.style.cursor = '';

            $('body').removeClass('measureCur').addClass('measureCur');

        }

        else {

            viewer.enableCursorStyle = true;

            $('body').removeClass('measureCur');

        }

    });

}
你这里面定义scene对象了吗?没有就用viewer.scene
我是点击了之后才触发,不是默认触发,但是我用LEFT_CLICK也不行,这是什么原因
...