一、簡介
繪制polyline、polygon,然后計算距離和面積,然后顯示標簽
雙擊結束繪制,右鍵結束繪制。
二、效果



三、部分代碼
/* * @Author: 蘋果園dog * @Date: 2021-01-09 16:17:38 * @LastEditTime: 2021-01-09 19:55:57 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \code\bayannaoer.web.vue\src\commonUtil\CesiumMeasure.js */ import cesiumMeasureUtil from '../utils/commonUtil/cesiumMeasureUtil'; /** * 繪制折線 * @param {*} viewer * @param {*} options */ function DrawPolyLine(viewer, options) { this.viewer = viewer; this.ismeare = options.ismeare || false; this.positions = []; this.poly = null; this.tooltip = document.getElementById(options.toolTip || "toolTip"); this.cartesian = null; this.floatingPoint = null; this.mouseHandler = null; this.onCompleted = options.onCompleted; this._pointLabels = []; this._totalLengthPointLabel = undefined; } /** * 清除繪制 */ DrawPolyLine.prototype.clear = function () { let removeLayerName = ["drawpolyline", "drawpolylinepoint"]; for (var i = 0; i < window.viewer.entities.values.length; i++) { var entity = window.viewer.entities.values[i]; if (removeLayerName.indexOf(entity.name) > -1) { window.viewer.entities.removeById(entity.id); i--; } } this.positions = []; this.poly = null; this.cartesian = null; this.floatingPoint = null; if (this.tooltip) { this.tooltip.style.display = "none"; } if (this.mouseHandler) { this.mouseHandler.destroy(); this.mouseHandler = null; } if (this.floatingPoint) { this.viewer.entities.remove(this.floatingPoint); } this._pointLabels = []; this._totalLengthPointLabel = undefined; }; /** * 開始繪制 */ DrawPolyLine.prototype.startDraw = function () { this.clear(); let tooltip = this.tooltip; this.ismeare = true; let ismeare = this.ismeare; let viewer = this.viewer; let cartesian = this.cartesian; let positions = this.positions; let floatingPoint = this.floatingPoint; this.mouseHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); let handler = this.mouseHandler; let poly = this.poly; let that = this; //鼠標移動事件 handler.setInputAction(function (movement) { if (!ismeare) { return; } tooltip.style.display = "block"; tooltip.style.left = movement.endPosition.x + 3 + "px"; tooltip.style.top = movement.endPosition.y - 25 + "px"; tooltip.innerHTML = "<p>單擊開始,雙擊結束,右鍵取消</p>"; let ray = viewer.camera.getPickRay(movement.endPosition); cartesian = viewer.scene.globe.pick(ray, viewer.scene); if (positions.length >= 2) { if (!Cesium.defined(poly)) { poly = new PolyLinePrimitive(positions); } else { positions.pop(); positions.push(cartesian); } that.showLengthLabel(); } viewer.scene.postProcessStages.fxaa.enabled = false; //去鋸齒 }, Cesium.ScreenSpaceEventType.MOUSE_MOVE); //鼠標左鍵單擊事件 handler.setInputAction(function (movement) { if (!ismeare) { return; } let ray = viewer.camera.getPickRay(movement.position); cartesian = viewer.scene.globe.pick(ray, viewer.scene); if (!cartesian) { return; } if (ismeare) { tooltip.style.display = "none"; if (!cartesian) { return; } if (positions.length == 0) { positions.push(cartesian.clone()); } positions.push(cartesian); floatingPoint = viewer.entities.add({ name: "drawpolylinepoint", position: positions[positions.length - 1], point: { pixelSize: 4, color: Cesium.Color.RED, outlineColor: Cesium.Color.WHITE, outlineWidth: 2, }, }); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); /** * 雙擊事件 */ handler.setInputAction(function (movement) { if (!ismeare) { return; } that.ismeare = false; that.positions.pop(); if (floatingPoint) { that.viewer.entities.remove(floatingPoint); } that.tooltip.style.display = "none"; that.viewer.selectedEntity = null; that.viewer.trackedEntity = null; if (that.onCompleted) { that.onCompleted(that.positions); } if (that.mouseHandler) { that.mouseHandler.destroy(); that.mouseHandler = null; } }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK); /** * 右鍵單擊事件取消繪制操作 */ handler.setInputAction(function (movement) { that.clear(); }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
