createDraw.js 2.09 KB
export default class createDraw {
    constructor(viewer) {
        this.viewer = viewer;
        this.handlerDis = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.Distance, 1);
        this.distance();
        this.handlerArea = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.Area, 0);
        this.area();
        this.handlerHeight = new Cesium.MeasureHandler(viewer, Cesium.MeasureMode.DVH);
        this.height();
    }
    distance() {
        this.handlerDis.measureEvt.addEventListener(result => {
            var dis = Number(result.distance), distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';
			this.handlerDis.disLabel.text = '距离:' + distance;
		});
		this.handlerDis.activeEvt.addEventListener(this.handle());
    }
    area() {
        this.handlerArea.measureEvt.addEventListener(result => {
			var mj = Number(result.area), area = mj > 1000000 ? (mj / 1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'
			this.handlerArea.areaLabel.text = '面积:' + area;
		});
		this.handlerArea.activeEvt.addEventListener(this.handle());
    }
    height() {
        this.handlerHeight.measureEvt.addEventListener(result => {
            var distance = result.distance > 1000 ? (result.distance / 1000).toFixed(2) + 'km' : result.distance + 'm',
                vHeight = result.verticalHeight > 1000 ? (result.verticalHeight / 1000).toFixed(2) + 'km' : result.verticalHeight + 'm',
                hDistance = result.horizontalDistance > 1000 ? (result.horizontalDistance / 1000).toFixed(2) + 'km' : result.horizontalDistance + 'm';
            this.handlerHeight.disLabel.text = '空间距离:' + distance;
            this.handlerHeight.vLabel.text = '垂直高度:' + vHeight;
            this.handlerHeight.hLabel.text = '水平距离:' + hDistance;
        });
        this.handlerArea.activeEvt.addEventListener(this.handle());
    }
    handle() {
        return a => {
            let body = $('body').removeClass('measureCur');
            a && body.addClass('measureCur');
            (this.viewer.enableCursorStyle = !a) || (this.viewer._element.style.cursor = '');
        }
    }
}