首页 > 大数据 > 可视化 > 可视化场景内任意绘制多边形并测量面积

appClick() {
let _this = this;
// 点击左键添加节点右键结束绘制
_this.opts.app.on('SingleClick', function(e) {
if (e.button == 0) {
if (!e.picked) return;
_this.numIndex++;
_this.ePosition = e.pickedPosition;
_this.createPoint(_this.ePosition);
_this.coordinatesArr.push(_this.ePosition);
_this.lineCoor.push(_this.ePosition);
_this.createLine(_this.coordinatesArr);
_this.getDistance();
_this.template =
`<div id="div` + _this.opts.modelNum + _this.numIndex + `" class="card-label card-label` + _this.opts.modelNum + `">
<span class="text">`;
if (_this.lineDistanceAll != null) {
_this.template += _this.lineDistanceAll + `米`;
} else {
_this.template += `<span style="color:#f45905; border-right: 1px solid #ccc;margin-right: 5px">` + _this.planeId + `</span> 起点`
}
_this.template +=
`</span>
<span><img id="points` + _this.opts.modelNum + _this.numIndex + `" src="/guide/examples/images/measure/remove.png"></span>
</div>`;
_this.boardId = 'div' + _this.opts.modelNum + _this.numIndex;
_this.createCard(_this.regionPoint);
_this.pointsObj = {
id: 'points' + _this.opts.modelNum + _this.numIndex, // 起点顶牌span标签id
parent: 'div' + _this.opts.modelNum + _this.numIndex, // 起点顶牌div标签id
coor: _this.opts.currPosition, // 起点坐标
distance: 0
};
_this.pointsObjArr.push(_this.pointsObj);
_this.cardClick();
} else {
if (_this.coordinatesArr.length < 2) {
_this.destroyAll();
_this.rianleyDom.css('display', 'none');
return;
};
_this.end();
}
_this.rianleyDom.css('display', 'none');
}, "点击");
// 鼠标移动持续绘制测量线段
_this.opts.app.on('MouseMove', function(e) {
if (e.picked) {
_this.ePosition = e.pickedPosition;
_this.pointsArr = [..._this.coordinatesArr, _this.ePosition];
_this.createLine(_this.pointsArr);
_this.line.style.color = '#f88020';
if (_this.pointsArr.length >= 2) {
_this.moveDistance = THING.Math.getDistance(_this.pointsArr[_this.pointsArr.length - 1], _this.pointsArr[_this.pointsArr.length - 2]);
let countNum = 0;
_this.disArr.forEach(v => {
countNum += parseFloat(v);
});
countNum = 1 * parseFloat(countNum).toFixed(2) + 1 * parseFloat(_this.moveDistance).toFixed(2);
_this.rianleyDom.css('display', 'block');
_this.rianleyDom.find('span.value').text(countNum.toFixed(2));
_this.rianleyDom.css('left', e.clientX + 10 + 'px');
_this.rianleyDom.css('top', e.clientY + 'px');
}
}
}, '移动');
// 结束绘制当前测量线段
_this.opts.app.on('DBLClick', function(ev) {
if (_this.coordinatesArr.length < 2) {
_this.destroyAll();
_this.rianleyDom.css('display', 'none');
return;
};
if (_this.coordinatesArr.length >= 3) {
_this.createPolygon(_this.coordinatesArr);
}
_this.end();
}, '双击');
}

1、 createPoint(ePosition) {
2、 var _this = this;
3、 _this.regionPoint = app.create({
4、 type: 'Sphere',
5、 id: 'points' + _this.opts.modelNum + _this.numIndex,
6、 name: 'points' + _this.opts.modelNum,
7、 radius: 0.2, // 半径
8、 widthSegments: 16,
9、 heightSegments: 16,
10、 position: ePosition, // 球体坐标
11、 style: {
12、 color: '#c10000',
13、 roughness: 50,
14、 opacity: 0.8
15、 }
16、 });
17、 }
18、 createLine(coordinates) {
19、 if (this.line) {
20、 this.line.destroy();
21、 }
22、 let id = this.opts.modelNum > 10 ? this.opts.modelNum : '0' + this.opts.modelNum;
23、 this.line = app.create({
24、 type: 'PolygonLine',
25、 name: 'line',
26、 id: 'line' + id,
27、 width: 0.03,
28、 points: coordinates,
29、 style: {
30、 image: '/guide/examples/images/measure/redLine.png',
31、 opacity: 0.9
32、 }
33、 });
34、 }
constructor(option) {
this.opts = option;
this.pointsArr = [this.opts.currPosition]; // 鼠标移动中坐标点的集合
this.coordinatesArr = [this.opts.currPosition]; // 存储鼠标点击后坐标点的集合
this.ePosition = null; // 存储触发事件后鼠标的位置
this.lineCoor = [this.opts.currPosition]; // 存储当前两个坐标点
this.disArr = []; // 存储所有坐标点与坐标点间的距离
this.numIndex = 0; // 自增变量
this.reSetDistance = 0; // 两点间的距离
this.lastStatus = false; // 判断是否绘制结束值为false为未结束true为结束
this.pointsObjArr = []; // 存储所有节点的id与顶牌id
this.rianleyDom = $('#marker'); // 跟随鼠标的提示
this.pointCardDom = $('#pointMarker'); // 鼠标移动至节点的提示
this.init();
this.appClick();
}
createTopCard(position) {
$('#div3d').append(this.template);
this.polygonCard = document.getElementById(this.boardId);
this.uiTop = app.create({
type: 'UIAnchor',
element: this.polygonCard,
position: [position[0], position[1], position[2]]
});
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69980539/viewspace-2786827/ ,如需转载,请注明出处,否则将追究法律责任。