Browse Source

增加单个删除功能,修改线的绘制逻辑

master
ctw 2 months ago
parent
commit
84cf5fd996
  1. 338
      ruoyi-ui/src/views/cesiumMap/index.vue

338
ruoyi-ui/src/views/cesiumMap/index.vue

@ -67,6 +67,7 @@ export default {
tempEntity: null, // tempEntity: null, //
tempPreviewEntity: null, // tempPreviewEntity: null, //
drawingPoints: [], drawingPoints: [],
drawingPointEntities: [], // 线
drawingStartPoint: null, drawingStartPoint: null,
isDrawing: false, isDrawing: false,
activeCursorPosition: null, // activeCursorPosition: null, //
@ -162,6 +163,8 @@ export default {
}) })
this.initScaleBar() this.initScaleBar()
this.initPointMovement()
this.initRightClickHandler()
console.log('Cesium离线二维地图已加载') console.log('Cesium离线二维地图已加载')
} catch (error) { } catch (error) {
@ -170,6 +173,52 @@ export default {
this.showErrorMessage(); this.showErrorMessage();
} }
}, },
initRightClickHandler() {
//
this.rightClickHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
//
this.rightClickHandler.setInputAction((click) => {
//
if (this.isDrawing) {
return;
}
const pickedObject = this.viewer.scene.pick(click.position)
if (Cesium.defined(pickedObject) && pickedObject.id) {
const pickedEntity = pickedObject.id
//
let entityData = this.allEntities.find(e => e.entity === pickedEntity || e === pickedEntity)
// 线线
if (!entityData) {
// 线
for (const lineEntity of this.allEntities) {
if (lineEntity.type === 'line' && lineEntity.pointEntities) {
if (lineEntity.pointEntities.includes(pickedEntity)) {
entityData = lineEntity
break
}
}
}
}
if (entityData) {
//
if (confirm('确定要删除这个对象吗?')) {
if (entityData.id) {
this.removeEntity(entityData.id)
} else if (entityData.entity && entityData.entity.id) {
this.removeEntity(entityData.entity.id)
}
}
}
}
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
},
showErrorMessage() { showErrorMessage() {
const container = document.getElementById('cesiumViewer'); const container = document.getElementById('cesiumViewer');
if (container) { if (container) {
@ -347,13 +396,49 @@ export default {
this.tempPreviewEntity = null; this.tempPreviewEntity = null;
} }
//
if (this.drawingPointEntities) {
this.drawingPointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity);
});
this.drawingPointEntities = [];
}
this.drawingPoints = []; this.drawingPoints = [];
this.drawingStartPoint = null; this.drawingStartPoint = null;
this.isDrawing = false; this.isDrawing = false;
this.activeCursorPosition = null; this.activeCursorPosition = null;
//
if (!this.rightClickHandler) {
this.initRightClickHandler();
}
this.viewer.scene.canvas.style.cursor = 'default'; this.viewer.scene.canvas.style.cursor = 'default';
}, },
cancelDrawing() {
//
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null;
}
if (this.drawingPointEntities) {
this.drawingPointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity);
});
this.drawingPointEntities = [];
}
this.drawingPoints = [];
this.activeCursorPosition = null;
},
// ******************************************************************** // ********************************************************************
// //
startPointDrawing() { startPointDrawing() {
@ -367,9 +452,9 @@ export default {
}, },
// 线 // 线
// 线
startLineDrawing() { startLineDrawing() {
this.drawingPoints = []; this.drawingPoints = [];
this.drawingPointEntities = []; //
// //
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity); if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
@ -377,7 +462,7 @@ export default {
this.tempEntity = null; this.tempEntity = null;
this.tempPreviewEntity = null; this.tempPreviewEntity = null;
// 1. // 1.
this.drawingHandler.setInputAction((movement) => { this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition); const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) { if (newPosition) {
@ -391,37 +476,29 @@ export default {
if (position) { if (position) {
this.drawingPoints.push(position); this.drawingPoints.push(position);
// === 线 === //
if (this.drawingPoints.length === 1) { this.entityCounter++;
this.activeCursorPosition = position; // const pointId = `point_${this.entityCounter}`;
const pointEntity = this.viewer.entities.add({
id: pointId,
position: position,
point: {
pixelSize: this.defaultStyles.point.size,
color: Cesium.Color.fromCssColorString(this.defaultStyles.point.color),
outlineColor: Cesium.Color.WHITE,
outlineWidth: 2
}
});
this.drawingPointEntities.push(pointEntity);
// 线 // 线
this.tempPreviewEntity = this.viewer.entities.add({ if (this.tempPreviewEntity) {
polyline: { this.viewer.entities.remove(this.tempPreviewEntity);
// 使 CallbackProperty this.tempPreviewEntity = null;
positions: new Cesium.CallbackProperty(() => {
//
if (this.drawingPoints.length > 0 && this.activeCursorPosition) {
//
const lastPoint = this.drawingPoints[this.drawingPoints.length - 1];
// [, ]
return [lastPoint, this.activeCursorPosition];
}
return [];
}, false),
width: this.defaultStyles.line.width,
// 线
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
dashLength: 16
}),
clampToGround: true //
}
});
} }
// === /线 ===
else { // 线
// 线线 if (this.drawingPoints.length > 1) {
if (this.tempEntity) { if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity); this.viewer.entities.remove(this.tempEntity);
} }
@ -434,20 +511,47 @@ export default {
} }
}); });
} }
// 线使 CallbackProperty
this.tempPreviewEntity = this.viewer.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(() => {
if (this.activeCursorPosition) {
return [this.drawingPoints[this.drawingPoints.length - 1], this.activeCursorPosition];
}
return [this.drawingPoints[this.drawingPoints.length - 1]];
}, false),
width: this.defaultStyles.line.width,
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
dashLength: 16
}),
clampToGround: true
}
});
} }
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// 3. // 3.
this.drawingHandler.setInputAction(() => { this.drawingHandler.setInputAction(() => {
// 线 //
if (this.tempPreviewEntity) { if (this.tempPreviewEntity) {
this.viewer.entities.remove(this.tempPreviewEntity); this.viewer.entities.remove(this.tempPreviewEntity);
this.tempPreviewEntity = null; this.tempPreviewEntity = null;
} }
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
}
if (this.drawingPoints.length > 1) { if (this.drawingPoints.length > 1) {
this.finishLineDrawing(); this.finishLineDrawing();
} else { } else {
//
this.drawingPointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity);
});
this.drawingPointEntities = [];
this.cancelDrawing(); this.cancelDrawing();
} }
@ -466,7 +570,7 @@ export default {
} }
// 线 // 线
const entity = this.addLineEntity([...this.drawingPoints]); const entity = this.addLineEntity([...this.drawingPoints], [...this.drawingPointEntities]);
// //
const length = this.calculateLineLength([...this.drawingPoints]); const length = this.calculateLineLength([...this.drawingPoints]);
@ -475,11 +579,19 @@ export default {
type: 'line' type: 'line'
}; };
// //
this.drawingPoints = []; this.drawingPoints = [];
this.drawingPointEntities = [];
this.tempEntity = null; this.tempEntity = null;
return entity; return entity;
} else { } else {
//
this.drawingPointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity);
});
this.drawingPointEntities = [];
this.cancelDrawing(); this.cancelDrawing();
return null; return null;
} }
@ -1548,7 +1660,7 @@ export default {
return entityData return entityData
}, },
addLineEntity(positions) { addLineEntity(positions, pointEntities = []) {
this.entityCounter++ this.entityCounter++
const id = `line_${this.entityCounter}` const id = `line_${this.entityCounter}`
@ -1569,6 +1681,7 @@ export default {
points: positions.map(p => this.cartesianToLatLng(p)), points: positions.map(p => this.cartesianToLatLng(p)),
positions: positions, positions: positions,
entity: entity, entity: entity,
pointEntities: pointEntities, //
color: this.defaultStyles.line.color, color: this.defaultStyles.line.color,
width: this.defaultStyles.line.width, width: this.defaultStyles.line.width,
label: `线 ${this.entityCounter}` label: `线 ${this.entityCounter}`
@ -1823,20 +1936,39 @@ export default {
}, },
removeEntity(id) { removeEntity(id) {
const index = this.allEntities.findIndex(e => e.id === id) //
const index = this.allEntities.findIndex(e =>
e.id === id ||
(e.entity && e.entity.id === id) ||
(e.type === 'line' && e.pointEntities && e.pointEntities.some(p => p.id === id))
)
if (index > -1) { if (index > -1) {
const entity = this.allEntities[index] const entity = this.allEntities[index]
// //
if (entity.entity) { if (entity instanceof Cesium.Entity) {
// A: Cesium Entity
this.viewer.entities.remove(entity)
} else if (entity.entity) {
// B: entity
this.viewer.entities.remove(entity.entity) this.viewer.entities.remove(entity.entity)
} }
// 线
if (entity.type === 'line' && entity.pointEntities) {
entity.pointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity)
})
}
// //
this.allEntities.splice(index, 1) this.allEntities.splice(index, 1)
// //
if (this.selectedEntity && this.selectedEntity.id === id) { if (this.selectedEntity && (this.selectedEntity.id === id || (this.selectedEntity.entity && this.selectedEntity.entity.id === id))) {
this.selectedEntity = null this.selectedEntity = null
} }
} }
@ -1861,6 +1993,13 @@ export default {
else if (item.id) { else if (item.id) {
this.viewer.entities.removeById(item.id); this.viewer.entities.removeById(item.id);
} }
// 线
if (item.type === 'line' && item.pointEntities) {
item.pointEntities.forEach(pointEntity => {
this.viewer.entities.remove(pointEntity);
});
}
} catch (e) { } catch (e) {
console.warn('删除实体失败:', e); console.warn('删除实体失败:', e);
} }
@ -2179,6 +2318,121 @@ export default {
// ... // ...
}, },
initPointMovement() {
//
this.pointMovementHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
let selectedPoint = null
let selectedLineEntity = null
let pointIndex = -1
let originalCameraController = null
let isMoving = false
//
this.pointMovementHandler.setInputAction((click) => {
const pickedObject = this.viewer.scene.pick(click.position)
if (Cesium.defined(pickedObject) && pickedObject.id) {
const pickedEntity = pickedObject.id
//
if (pickedEntity.point) {
// 线
for (const lineEntity of this.allEntities) {
if (lineEntity.type === 'line' && lineEntity.pointEntities) {
const index = lineEntity.pointEntities.indexOf(pickedEntity)
if (index !== -1) {
selectedPoint = pickedEntity
selectedLineEntity = lineEntity
pointIndex = index
isMoving = true
// 使
originalCameraController = this.viewer.scene.screenSpaceCameraController.enableInputs
this.viewer.scene.screenSpaceCameraController.enableInputs = false
break
}
}
}
}
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN)
//
this.pointMovementHandler.setInputAction((movement) => {
if (isMoving && selectedPoint && selectedLineEntity) {
const newPosition = this.getClickPosition(movement.endPosition)
if (newPosition) {
//
selectedPoint.position = newPosition
// Cesium
const newPositions = [...selectedLineEntity.positions]
newPositions[pointIndex] = newPosition
// 线
this.viewer.entities.remove(selectedLineEntity.entity)
// 线
const entitiesToRemove = []
this.viewer.entities.values.forEach(e => {
if (e.id && e.id === selectedLineEntity.id) {
entitiesToRemove.push(e)
}
})
entitiesToRemove.forEach(e => {
this.viewer.entities.remove(e)
})
// 线
const newEntity = this.viewer.entities.add({
id: selectedLineEntity.id,
name: selectedLineEntity.label,
polyline: {
positions: newPositions,
width: selectedLineEntity.width,
material: Cesium.Color.fromCssColorString(selectedLineEntity.color),
clampToGround: true
}
})
// 线
selectedLineEntity.entity = newEntity
selectedLineEntity.positions = newPositions
//
selectedLineEntity.points[pointIndex] = this.cartesianToLatLng(newPosition)
//
const length = this.calculateLineLength(selectedLineEntity.positions)
this.measurementResult = {
distance: length,
type: 'line'
}
//
this.viewer.scene.requestRender()
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
//
this.pointMovementHandler.setInputAction(() => {
//
if (originalCameraController !== null) {
this.viewer.scene.screenSpaceCameraController.enableInputs = originalCameraController
originalCameraController = null
}
isMoving = false
selectedPoint = null
selectedLineEntity = null
pointIndex = -1
}, Cesium.ScreenSpaceEventType.LEFT_UP)
},
updateScaleBar() { updateScaleBar() {
// ... // ...
}, },
@ -2187,6 +2441,16 @@ export default {
this.stopDrawing() this.stopDrawing()
this.clearAll() this.clearAll()
if (this.pointMovementHandler) {
this.pointMovementHandler.destroy()
this.pointMovementHandler = null
}
if (this.rightClickHandler) {
this.rightClickHandler.destroy()
this.rightClickHandler = null
}
if (this.viewer) { if (this.viewer) {
this.viewer.destroy() this.viewer.destroy()
this.viewer = null this.viewer = null

Loading…
Cancel
Save