我这个是用vue写的,代码如下
//初始化方法 sightInitializing(tab) { sightline = new Cesium.Sightline(Viewer.scene); sightline.couldRemove = false; sgPointHandler = new Cesium.PointHandler(Viewer); let that = this; sgPointHandler.drawCompletedEvent.addEventListener((point) => { if (!viewPointFlag) { this.$message.warning('未添加观察点!'); return; } let position = point.position._value; sightline.build(); //将获取的点的位置转化成经纬度 let cartographic = Cesium.Cartographic.fromCartesian(position); let longitude = Cesium.Math.toDegrees(cartographic.longitude); let latitude = Cesium.Math.toDegrees(cartographic.latitude); let height = cartographic.height; if(Viewer.scene.sgFlag) {// 添加观察点 //设置视口位置 sightline.viewPosition = [longitude, latitude, height]; Viewer.scene.sgFlag = false; that.pointX = longitude; that.pointY = latitude; that.pointZ = height; } else {// 添加目标点 // if (sightline.couldRemove) { // sightline.removeAllTargetPoint(); // } Viewer.entities.remove(point); //添加视点 sightline.addTargetPoint({ position : [longitude, latitude, height], name : "point" + new Date() }); sightline.couldRemove = true; } //选择完观察点/目标点后,继续添加下一个目标点 sgPointHandler.deactivate(); that.$message.info('请选择目标点') that.addTargetPoint() let tcolor = Cesium.Color.fromCssColorString(that.trueColor); sightline.visibleColor = tcolor; let utcolor = Cesium.Color.fromCssColorString(that.errorCorlr); sightline.hiddenColor = utcolor; }); //默认可以直接选择观察点 this.$message.info('请选择观察点') this.addViewpoint(); }, addViewpoint() { viewPointFlag = true; if(sgPointHandler.active) { return; } Viewer.scene.sgFlag = true; Viewer.entities.removeAll(); if(sightline.couldRemove) { sightline.removeAllTargetPoint(); } sgPointHandler.activate(); }, addTargetPoint() { Viewer.scene.sgFlag = false; sgPointHandler.activate(); },
<!--StartFragment -->
PointHandler的绘制绘制完成事件有问题,建议不用PointHandler去绘制点,注册左键事件直接获取场景坐标。