Browse Source

优化所有地图绘制

master
ctw 2 months ago
parent
commit
65ed6b13d9
  1. 327
      ruoyi-ui/src/views/cesiumMap/index.vue

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

@ -69,6 +69,7 @@ export default {
drawingPoints: [],
drawingStartPoint: null,
isDrawing: false,
activeCursorPosition: null, //
//
allEntities: [], //
@ -349,6 +350,7 @@ export default {
this.drawingPoints = [];
this.drawingStartPoint = null;
this.isDrawing = false;
this.activeCursorPosition = null;
this.viewer.scene.canvas.style.cursor = 'default';
},
@ -368,8 +370,6 @@ export default {
// 线
startLineDrawing() {
this.drawingPoints = [];
//
let activeCursorPosition = null;
//
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
@ -381,7 +381,7 @@ export default {
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
this.activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
@ -393,7 +393,7 @@ export default {
// === 线 ===
if (this.drawingPoints.length === 1) {
activeCursorPosition = position; //
this.activeCursorPosition = position; //
// 线
this.tempPreviewEntity = this.viewer.entities.add({
@ -401,11 +401,11 @@ export default {
// 使 CallbackProperty
positions: new Cesium.CallbackProperty(() => {
//
if (this.drawingPoints.length > 0 && activeCursorPosition) {
if (this.drawingPoints.length > 0 && this.activeCursorPosition) {
//
const lastPoint = this.drawingPoints[this.drawingPoints.length - 1];
// [, ]
return [lastPoint, activeCursorPosition];
return [lastPoint, this.activeCursorPosition];
}
return [];
}, false),
@ -451,8 +451,8 @@ export default {
this.cancelDrawing();
}
//
activeCursorPosition = null;
//
this.activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
@ -475,7 +475,9 @@ export default {
type: 'line'
};
this.stopDrawing();
//
this.drawingPoints = [];
this.tempEntity = null;
return entity;
} else {
this.cancelDrawing();
@ -486,7 +488,6 @@ export default {
//
startPolygonDrawing() {
this.drawingPoints = [];
let activeCursorPosition = null; //
// 1.
//
@ -499,7 +500,7 @@ export default {
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
this.activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
@ -511,7 +512,7 @@ export default {
// === ===
if (this.drawingPoints.length === 1) {
activeCursorPosition = position; //
this.activeCursorPosition = position; //
this.tempEntity = this.viewer.entities.add({
// --- ---
@ -519,8 +520,8 @@ export default {
// hierarchy 使 CallbackProperty
hierarchy: new Cesium.CallbackProperty(() => {
// +
if (activeCursorPosition) {
return new Cesium.PolygonHierarchy([...this.drawingPoints, activeCursorPosition]);
if (this.activeCursorPosition) {
return new Cesium.PolygonHierarchy([...this.drawingPoints, this.activeCursorPosition]);
}
return new Cesium.PolygonHierarchy(this.drawingPoints);
}, false),
@ -533,9 +534,9 @@ export default {
polyline: {
// positions 使 CallbackProperty 线
positions: new Cesium.CallbackProperty(() => {
if (activeCursorPosition) {
if (this.activeCursorPosition) {
// [, , ]
return [...this.drawingPoints, activeCursorPosition, this.drawingPoints[0]];
return [...this.drawingPoints, this.activeCursorPosition, this.drawingPoints[0]];
}
return this.drawingPoints;
}, false),
@ -561,7 +562,7 @@ export default {
}
//
activeCursorPosition = null;
this.activeCursorPosition = null;
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
@ -576,15 +577,19 @@ export default {
type: 'polygon'
}
this.stopDrawing()
//
this.drawingPoints = []
if (this.tempEntity) {
this.viewer.entities.remove(this.tempEntity)
this.tempEntity = null
}
return entity
},
//
startRectangleDrawing() {
//
this.drawingPoints = []; //
let activeCursorPosition = null; //
let startPoint = null; //
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
@ -594,7 +599,7 @@ export default {
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
this.activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
@ -603,19 +608,18 @@ export default {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (!startPoint) {
startPoint = position;
this.drawingPoints.push(startPoint);
activeCursorPosition = startPoint; //
if (this.drawingPoints.length === 0) {
this.drawingPoints.push(position);
this.activeCursorPosition = position; //
//
this.tempEntity = this.viewer.entities.add({
rectangle: {
// 使 CallbackProperty
coordinates: new Cesium.CallbackProperty(() => {
if (startPoint && activeCursorPosition) {
if (this.drawingPoints.length > 0 && this.activeCursorPosition) {
// 使 Cesium
return Cesium.Rectangle.fromCartesianArray([startPoint, activeCursorPosition]);
return Cesium.Rectangle.fromCartesianArray([this.drawingPoints[0], this.activeCursorPosition]);
}
return Cesium.Rectangle.fromDegrees(0, 0, 0, 0);
}, false),
@ -629,10 +633,10 @@ export default {
});
}
// --- B ---
else {
else if (this.drawingPoints.length === 1) {
this.drawingPoints.push(position);
//
activeCursorPosition = null;
this.activeCursorPosition = null;
//
this.finishRectangleDrawing();
@ -679,8 +683,8 @@ export default {
type: 'rectangle'
};
// 6.
this.stopDrawing();
// 6.
this.drawingPoints = [];
},
//
@ -718,73 +722,136 @@ export default {
},
//
startCircleDrawing() {
//
this.drawingPoints = []; //
let activeCursorPosition = null; //
let centerPoint = null; //
this.activeCursorPosition = null;
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
this.tempEntity = null;
if (this.tempEntity) {
try {
this.viewer.entities.remove(this.tempEntity);
} catch (e) {
console.warn('Failed to remove temp entity:', e);
}
this.tempEntity = null;
}
if (this.tempPreviewEntity) {
try {
this.viewer.entities.remove(this.tempPreviewEntity);
} catch (e) {
console.warn('Failed to remove temp preview entity:', e);
}
this.tempPreviewEntity = null;
}
// 2.
// 2.
this.drawingHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.drawingHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
this.drawingHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
// 3.
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
try {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
this.activeCursorPosition = newPosition;
}
} catch (e) {
console.warn('Mouse move error:', e);
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
// 3.
// 4.
this.drawingHandler.setInputAction((click) => {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (!centerPoint) {
centerPoint = position;
this.drawingPoints.push(centerPoint);
activeCursorPosition = centerPoint;
try {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (this.drawingPoints.length === 0) {
this.drawingPoints.push(position);
this.activeCursorPosition = position;
//
if (this.tempEntity) {
try {
this.viewer.entities.remove(this.tempEntity);
} catch (e) {
console.warn('Failed to remove existing temp entity:', e);
}
this.tempEntity = null;
}
//
this.tempEntity = this.viewer.entities.add({
position: centerPoint, //
ellipse: {
// 使 CallbackProperty
semiMajorAxis: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
return Cesium.Cartesian3.distance(centerPoint, activeCursorPosition);
}
return 0;
}, false),
semiMinorAxis: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
return Cesium.Cartesian3.distance(centerPoint, activeCursorPosition);
//
if (position) {
// 0
const initialRadius = 100; // 100
this.tempEntity = this.viewer.entities.add({
position: position, // 使
ellipse: {
// 使 CallbackProperty
semiMajorAxis: new Cesium.CallbackProperty(() => {
try {
if (this.activeCursorPosition && this.drawingPoints.length > 0 && this.drawingPoints[0]) {
const center = this.drawingPoints[0];
const edge = this.activeCursorPosition;
if (center && edge && typeof center.x === 'number' && typeof edge.x === 'number') {
const distance = Cesium.Cartesian3.distance(center, edge);
return isFinite(distance) && distance > 0 ? distance : initialRadius;
}
}
return initialRadius;
} catch (e) {
return initialRadius;
}
}, false),
semiMinorAxis: new Cesium.CallbackProperty(() => {
try {
if (this.activeCursorPosition && this.drawingPoints.length > 0 && this.drawingPoints[0]) {
const center = this.drawingPoints[0];
const edge = this.activeCursorPosition;
if (center && edge && typeof center.x === 'number' && typeof edge.x === 'number') {
const distance = Cesium.Cartesian3.distance(center, edge);
return isFinite(distance) && distance > 0 ? distance : initialRadius;
}
}
return initialRadius;
} catch (e) {
return initialRadius;
}
}, false),
//
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2,
// height: 0, // 使 heightReference
}
return 0;
}, false),
//
material: Cesium.Color.fromCssColorString(this.defaultStyles.polygon.color).withAlpha(0.5),
outline: true,
outlineColor: Cesium.Color.fromCssColorString(this.defaultStyles.line.color),
outlineWidth: 2,
// height: 0, // 使 heightReference
});
}
});
}
// --- B/ ---
else {
// 便
this.drawingPoints.push(position);
activeCursorPosition = null; //
//
this.finishCircleDrawing(position);
}
// --- B/ ---
else if (this.drawingPoints.length === 1) {
// 便
this.drawingPoints.push(position);
this.activeCursorPosition = null; //
//
this.finishCircleDrawing(position);
}
}
} catch (e) {
console.warn('Mouse click error:', e);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
// 4.
// 5.
this.drawingHandler.setInputAction(() => {
this.cancelDrawing();
try {
this.cancelDrawing();
} catch (e) {
console.warn('Right click error:', e);
}
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},
@ -827,17 +894,15 @@ export default {
type: 'circle'
};
// 6.
this.stopDrawing();
// 6.
this.drawingPoints = [];
this.activeCursorPosition = null;
},
//
startSectorDrawing() {
//
this.drawingPoints = []; //
let activeCursorPosition = null; //
let centerPoint = null; //
let radiusPoint = null; //
let radius = 0; //
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
@ -849,7 +914,7 @@ export default {
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
this.activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
@ -858,17 +923,16 @@ export default {
const position = this.getClickPosition(click.position);
if (position) {
// --- A ---
if (!centerPoint) {
centerPoint = position;
this.drawingPoints.push(centerPoint);
activeCursorPosition = centerPoint;
if (this.drawingPoints.length === 0) {
this.drawingPoints.push(position);
this.activeCursorPosition = position;
// 线
this.tempPreviewEntity = this.viewer.entities.add({
polyline: {
positions: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
return [centerPoint, activeCursorPosition];
if (this.drawingPoints.length > 0 && this.activeCursorPosition) {
return [this.drawingPoints[0], this.activeCursorPosition];
}
return [];
}, false),
@ -882,10 +946,12 @@ export default {
});
}
// --- B ---
else if (!radiusPoint) {
radiusPoint = position;
this.drawingPoints.push(radiusPoint);
radius = Cesium.Cartesian3.distance(centerPoint, radiusPoint);
else if (this.drawingPoints.length === 1) {
this.drawingPoints.push(position);
this.activeCursorPosition = position; // activeCursorPosition
const centerPoint = this.drawingPoints[0];
const radiusPoint = this.drawingPoints[1];
const fixedRadius = Cesium.Cartesian3.distance(centerPoint, radiusPoint);
// 线
if (this.tempPreviewEntity) {
@ -897,11 +963,15 @@ export default {
this.tempEntity = this.viewer.entities.add({
polygon: {
hierarchy: new Cesium.CallbackProperty(() => {
if (centerPoint && activeCursorPosition) {
const currentRadius = Cesium.Cartesian3.distance(centerPoint, activeCursorPosition);
if (this.drawingPoints.length > 1 && this.activeCursorPosition) {
const centerPoint = this.drawingPoints[0];
const radiusPoint = this.drawingPoints[1];
if (!isFinite(fixedRadius) || fixedRadius === 0) {
return new Cesium.PolygonHierarchy([]);
}
const startAngle = this.calculatePointAngle(centerPoint, radiusPoint);
const endAngle = this.calculatePointAngle(centerPoint, activeCursorPosition);
const positions = this.generateSectorPositions(centerPoint, currentRadius, startAngle, endAngle);
const endAngle = this.calculatePointAngle(centerPoint, this.activeCursorPosition);
const positions = this.generateSectorPositions(centerPoint, fixedRadius, startAngle, endAngle);
return new Cesium.PolygonHierarchy(positions);
}
return new Cesium.PolygonHierarchy([]);
@ -914,13 +984,12 @@ export default {
});
}
// --- C ---
else {
const anglePoint = position;
this.drawingPoints.push(anglePoint);
activeCursorPosition = null; //
else if (this.drawingPoints.length === 2) {
this.drawingPoints.push(position);
this.activeCursorPosition = null; //
//
this.finishSectorDrawing(centerPoint, radiusPoint, anglePoint);
this.finishSectorDrawing(this.drawingPoints[0], this.drawingPoints[1], this.drawingPoints[2]);
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
@ -933,7 +1002,7 @@ export default {
//
finishSectorDrawing(centerPoint, radiusPoint, anglePoint) {
const radius = Cesium.Cartesian3.distance(centerPoint, anglePoint);
const radius = Cesium.Cartesian3.distance(centerPoint, radiusPoint);
const startAngle = this.calculatePointAngle(centerPoint, radiusPoint);
const endAngle = this.calculatePointAngle(centerPoint, anglePoint);
@ -978,8 +1047,8 @@ export default {
};
this.allEntities.push(entityData);
// 5.
this.stopDrawing();
// 5.
this.drawingPoints = [];
},
//
@ -989,8 +1058,8 @@ export default {
const centerLL = Cesium.Cartographic.fromCartesian(center);
//
const startAngle = Math.atan2(startLL.longitude - centerLL.longitude, startLL.latitude - centerLL.latitude);
const endAngle = Math.atan2(endLL.longitude - centerLL.longitude, endLL.latitude - centerLL.latitude);
const startAngle = Math.atan2(startLL.latitude - centerLL.latitude, startLL.longitude - centerLL.longitude);
const endAngle = Math.atan2(endLL.latitude - centerLL.latitude, endLL.longitude - centerLL.longitude);
//
return endAngle - startAngle;
@ -1003,8 +1072,8 @@ export default {
const centerLL = Cesium.Cartographic.fromCartesian(center);
//
const startAngle = Math.atan2(startLL.longitude - centerLL.longitude, startLL.latitude - centerLL.latitude);
const endAngle = Math.atan2(endLL.longitude - centerLL.longitude, endLL.latitude - centerLL.latitude);
const startAngle = Math.atan2(startLL.latitude - centerLL.latitude, startLL.longitude - centerLL.longitude);
const endAngle = Math.atan2(endLL.latitude - centerLL.latitude, endLL.longitude - centerLL.longitude);
//
let angleDiff = endAngle - startAngle;
@ -1022,7 +1091,7 @@ export default {
const centerLL = Cesium.Cartographic.fromCartesian(center);
//
const angle = Math.atan2(pointLL.longitude - centerLL.longitude, pointLL.latitude - centerLL.latitude);
const angle = Math.atan2(pointLL.latitude - centerLL.latitude, pointLL.longitude - centerLL.longitude);
return angle;
},
@ -1034,8 +1103,8 @@ export default {
//
positions.push(center);
//
let angleDiff = endAngle - startAngle;
//
let angleDiff = startAngle - endAngle;
if (angleDiff < 0) {
angleDiff += 2 * Math.PI;
}
@ -1047,13 +1116,13 @@ export default {
const numPoints = Math.max(5, Math.ceil(angleDiff * 180 / Math.PI / 10));
const angleStep = angleDiff / (numPoints - 1);
//
//
for (let i = 0; i < numPoints; i++) {
const currentAngle = startAngle + i * angleStep;
const currentAngle = startAngle - i * angleStep;
const distance = radius / 6378137; //
const lat = centerLL.latitude + Math.cos(currentAngle) * distance;
const lng = centerLL.longitude + Math.sin(currentAngle) * distance / Math.cos(centerLL.latitude);
const lat = centerLL.latitude + Math.sin(currentAngle) * distance;
const lng = centerLL.longitude + Math.cos(currentAngle) * distance / Math.cos(centerLL.latitude);
const position = Cesium.Cartesian3.fromRadians(lng, lat);
positions.push(position);
@ -1065,6 +1134,8 @@ export default {
return positions;
},
//
calculateDistance(point1, point2) {
return Cesium.Cartesian3.distance(point1, point2);
@ -1073,7 +1144,6 @@ export default {
//
startArrowDrawing() {
this.drawingPoints = []; //
let activeCursorPosition = null; //
// 1.
if (this.tempEntity) this.viewer.entities.remove(this.tempEntity);
@ -1085,7 +1155,7 @@ export default {
this.drawingHandler.setInputAction((movement) => {
const newPosition = this.getClickPosition(movement.endPosition);
if (newPosition) {
activeCursorPosition = newPosition;
this.activeCursorPosition = newPosition;
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
@ -1097,7 +1167,7 @@ export default {
// --- A ---
if (this.drawingPoints.length === 1) {
activeCursorPosition = position; //
this.activeCursorPosition = position; //
//
this.tempPreviewEntity = this.viewer.entities.add({
@ -1105,11 +1175,11 @@ export default {
// 使 CallbackProperty
positions: new Cesium.CallbackProperty(() => {
//
if (this.drawingPoints.length > 0 && activeCursorPosition) {
if (this.drawingPoints.length > 0 && this.activeCursorPosition) {
//
const lastPoint = this.drawingPoints[this.drawingPoints.length - 1];
// [, ]
return [lastPoint, activeCursorPosition];
return [lastPoint, this.activeCursorPosition];
}
return [];
}, false),
@ -1126,7 +1196,7 @@ export default {
// --- B ---
else {
//
activeCursorPosition = null;
this.activeCursorPosition = null;
//
this.finishArrowDrawing();
@ -1153,7 +1223,8 @@ export default {
//
const entity = this.addArrowEntity([...this.drawingPoints]);
this.stopDrawing();
//
this.drawingPoints = [];
return entity;
} else {
this.cancelDrawing();

Loading…
Cancel
Save