visual.js 3.82 KB
import objectManage from './maputils';
export default class visual {
    constructor() {
        objectManage.viewer.scene.viewFlag = true;
        this.pointHandler = new Cesium.DrawHandler(objectManage.viewer, Cesium.DrawMode.Point);
        this.viewshed3D = new Cesium.ViewShed3D(objectManage.viewer.scene);
        this.viewPosition;
        this.viewModel = {
            direction: 1.0,
            pitch: 1.0,
            distance: 1.0,
            verticalFov: 1.0,
            horizontalFov: 1.0,
            visibleAreaColor: '#ffffffff',
            invisibleAreaColor: '#ffffffff'
        };
        Cesium.knockout.track(this.viewModel);
        this.init(objectManage.viewer.scene);
    }
    init(scene) {
        this.handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
        this.handler.setInputAction(e => {
            // 若此标记为false,则激活对可视域分析对象的操作
            if (!scene.viewFlag) {
                //获取鼠标屏幕坐标,并将其转化成笛卡尔坐标
                var position = e.endPosition;
                var last = scene.pickPosition(position);
                //计算该点与视口位置点坐标的距离
                var distance = Cesium.Cartesian3.distance(this.viewPosition, last);
                if (distance > 0) {
                    // 将鼠标当前点坐标转化成经纬度
                    var cartographic = Cesium.Cartographic.fromCartesian(last);
                    var longitude = Cesium.Math.toDegrees(cartographic.longitude);
                    var latitude = Cesium.Math.toDegrees(cartographic.latitude);
                    var height = cartographic.height;
                    // 通过该点设置可视域分析对象的距离及方向
                    this.viewshed3D.setDistDirByPoint([longitude, latitude, height]);
                }
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this.handler.setInputAction(e => {
            //鼠标右键事件回调,不再执行鼠标移动事件中对可视域的操作
            scene.viewFlag = true;
            Object.assign(this.viewModel, {
                direction: this.viewshed3D.direction, pitch: this.viewshed3D.pitch, distance: this.viewshed3D.distance, 
                horizontalFov: this.viewshed3D.horizontalFov, verticalFov: this.viewshed3D.verticalFov
            })
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        this.pointHandler.drawEvt.addEventListener(result => {
            var point = result.object;
            var position = point.position;
            this.viewPosition = position;
            // 将获取的点的位置转化成经纬度
            var cartographic = Cesium.Cartographic.fromCartesian(position);
            var longitude = Cesium.Math.toDegrees(cartographic.longitude);
            var latitude = Cesium.Math.toDegrees(cartographic.latitude);
            var height = cartographic.height + 1.8;
            point.position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
            if (objectManage.viewer.scene.viewFlag) {
                // 设置视口位置
                this.viewshed3D.viewPosition = [longitude, latitude, height];
                this.viewshed3D.build();
                // 将标记置为false以激活鼠标移动回调里面的设置可视域操作
                objectManage.viewer.scene.viewFlag = false;
            }
        });
    }
    chooseView() {
        if (this.pointHandler.active) return;
        //先清除之前的可视域分析
        objectManage.viewer.entities.removeAll();
        this.viewshed3D.distance = 0.1;
        objectManage.viewer.scene.viewFlag = true;
        //激活绘制点类
        this.pointHandler.activate();
    }
    remove() {
        this.viewshed3D.destroy();
        this.viewshed3D = new Cesium.ViewShed3D(objectManage.viewer.scene);
    }
}