绘制前显示正常
调用绘制方法绘制时
绘制函数:
startDraw() {
var that = this;
this.viewer.scene.globe.depthTestAgainstTerrain = true; //深度检测
//鼠标左键
this.handler.setInputAction(function (event) {
//点击获取经纬度坐标
var earthPosition = that.viewer.scene.pickPosition(event.position);
if (Cesium.defined(earthPosition)) {
if (that.activeShapePointsLatlng.length === 0) {
that.floatingPoint = that.createPoint(earthPosition);
that.activeShapePointsLatlng.push(earthPosition);
var dynamicPositions = new Cesium.CallbackProperty(function () {
if (that.btntext === "casual") {
return new Cesium.PolygonHierarchy(
that.activeShapePointsLatlng
);
}
return that.activeShapePointsLatlng;
}, false);
that.activeShape = that.drawGraphic(dynamicPositions); //绘制动态图
}
that.activeShapePointsLatlng.push(earthPosition);
that.activeShapePoints.push(that.createPoint(earthPosition));
that.entityType == "point"
? that.pointArr.push(that.createPoint(earthPosition))
: null;
}
//-----------------------------
let cartesian = viewer.camera.pickEllipsoid(event.position);
let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
let lng = Cesium.Math.toDegrees(cartographic.longitude); // 经度
let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度
let alt = cartographic.height; // 高度,椭球面height永远等于0
let coordinate = {
longitude: Number(lng.toFixed(6)),
latitude: Number(lat.toFixed(6)),
altitude: Number(alt.toFixed(2)),
};
console.log(coordinate, "111111111111111111");
//-------------------------------
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
//鼠标移动
that.handler.setInputAction(function (event) {
if (Cesium.defined(that.floatingPoint)) {
var newPosition = that.viewer.scene.pickPosition(event.endPosition);
if (Cesium.defined(newPosition)) {
that.floatingPoint.position.setValue(newPosition);
that.activeShapePointsLatlng.pop();
that.activeShapePointsLatlng.push(newPosition);
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// 鼠标右击
this.handler.setInputAction(function (event) {
//点击获取经纬度坐标
var earthPosition = that.viewer.scene.pickPosition(event.position);
if (Cesium.defined(earthPosition)) {
if (that.activeShapePointsLatlng.length === 0) {
that.floatingPoint = that.createPoint(earthPosition);
that.activeShapePointsLatlng.push(earthPosition);
var dynamicPositions = new Cesium.CallbackProperty(function () {
if (that.btntext === "casual") {
return new Cesium.PolygonHierarchy(
that.activeShapePointsLatlng
);
}
return that.activeShapePointsLatlng;
}, false);
that.activeShape = that.drawGraphic(dynamicPositions); //绘制动态图
}
that.activeShapePointsLatlng.push(earthPosition);
that.activeShapePoints.push(that.createPoint(earthPosition));
that.entityType == "point"
? that.pointArr.push(that.createPoint(earthPosition))
: null;
}
//-----------------------------
let cartesian = viewer.camera.pickEllipsoid(event.position);
let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
let lng = Cesium.Math.toDegrees(cartographic.longitude); // 经度
let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度
let alt = cartographic.height; // 高度,椭球面height永远等于0
let coordinate = {
longitude: Number(lng.toFixed(6)),
latitude: Number(lat.toFixed(6)),
altitude: Number(alt.toFixed(2)),
};
//-------------------------------
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
// Redraw the shape so it's not dynamic and remove the dynamic shape.
function terminateShape() {
that.activeShapePointsLatlng.pop(); //去除最后一个动态点
var tempArr;
//把实体存入数组
switch (that.btntext) {
// case "polyline":
// tempArr=that.polylineArr
// break
case "casual":
tempArr = that.polygonArr; //多边形
break;
case "round":
tempArr = that.ellipseArr; //圆
break;
case "rectangle":
tempArr = that.rectangleArr;
break;
}
if (that.activeShapePointsLatlng.length) {
tempArr.push(that.drawGraphic(that.activeShapePointsLatlng)); //绘制最终图
}
//移除节点
if (that.activeShapePoints.length) {
that.activeShapePoints.filter((point) => {
that.viewer.entities.remove(point);
});
}
that.viewer.entities.remove(that.floatingPoint); //去除动态点图形(当前鼠标点)
that.viewer.entities.remove(that.activeShape); //去除动态图形
that.floatingPoint = undefined;
that.activeShape = undefined;
that.activeShapePointsLatlng = [];
}
//鼠标右键移除动态图层
that.handler.setInputAction(function (event) {
terminateShape();
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},