1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
| <template> <div class="cesium-container"> <div class="cesium-map" id="cesiumMap"></div> <div class="toolbar"> <button @click="setDrawMode('none')" :class="{ active: drawMode === 'none' }" > 浏览模式 </button> <button @click="setDrawMode('point')" :class="{ active: drawMode === 'point' }" > 绘制点 </button> <button @click="setDrawMode('line')" :class="{ active: drawMode === 'line' }" > 绘制线 </button> <button @click="setDrawMode('polygon')" :class="{ active: drawMode === 'polygon' }" > 绘制面 </button> <button @click="clearDrawings">清除绘制</button> </div> <div class="info-panel" v-if="currentPosition"> <div>经度: {{ currentPosition.longitude.toFixed(6) }}</div> <div>纬度: {{ currentPosition.latitude.toFixed(6) }}</div> <div>高度: {{ currentPosition.height.toFixed(2) }}m</div> </div> </div> </template>
<script setup> import { ref, onMounted, onUnmounted } from "vue"; import { Viewer, ScreenSpaceEventHandler, ScreenSpaceEventType, Cartesian3, Cartesian2, Cartographic, Color, Math, defined, PolygonHierarchy, HeightReference, LabelStyle, HorizontalOrigin, VerticalOrigin, } from "cesium"; import * as turf from "@turf/turf";
let viewer = null; let handler = null;
const drawMode = ref("none"); const confirmedPoints = ref([]); // 已经左键点击确认的点 const currentPosition = ref(null);
// 临时绘制相关(鼠标移动时的预览) let tempShapeEntity = null; let tempLabelEntity = null;
onMounted(() => { viewer = new Viewer("cesiumMap", { geocoder: false, homeButton: false, sceneModePicker: false, baseLayerPicker: false, navigationHelpButton: false, animation: false, timeline: false, fullscreenButton: false, vrButton: false, infoBox: false, selectionIndicator: false, });
handler = new ScreenSpaceEventHandler(viewer.scene.canvas);
// 1. 左键点击:确认一个点 handler.setInputAction((event) => { if (drawMode.value === "none") return;
const cartesian = viewer.camera.pickEllipsoid( event.position, viewer.scene.globe.ellipsoid, );
if (defined(cartesian)) { const cartographic = Cartographic.fromCartesian(cartesian); const longitude = Math.toDegrees(cartographic.longitude); const latitude = Math.toDegrees(cartographic.latitude); const height = cartographic.height;
currentPosition.value = { longitude, latitude, height };
// 绘制一个永久的红色顶点标记 viewer.entities.add({ position: cartesian, point: { pixelSize: 8, color: Color.RED, outlineColor: Color.WHITE, outlineWidth: 2, }, });
handlePointConfirmed(cartesian, longitude, latitude); } }, ScreenSpaceEventType.LEFT_CLICK);
// 2. 鼠标移动:更新预览 handler.setInputAction((event) => { if ( drawMode.value === "none" || drawMode.value === "point" || confirmedPoints.value.length === 0 ) { return; }
const cartesian = viewer.camera.pickEllipsoid( event.endPosition, viewer.scene.globe.ellipsoid, );
if (defined(cartesian)) { updatePreview([...confirmedPoints.value, cartesian]); } }, ScreenSpaceEventType.MOUSE_MOVE);
// 3. 右键点击:完成绘制,创建永久实体 handler.setInputAction(() => { if (drawMode.value !== "none" && confirmedPoints.value.length > 0) { finishDrawing(); } }, ScreenSpaceEventType.RIGHT_CLICK); });
onUnmounted(() => { if (handler) handler.destroy(); if (viewer) viewer.destroy(); });
// 坐标转换:Cesium Cartesian3 数组 -> Turf [lng, lat] 数组 function cartesiansToTurfCoords(cartesians) { return cartesians.map((c) => { const carto = Cartographic.fromCartesian(c); return [Math.toDegrees(carto.longitude), Math.toDegrees(carto.latitude)]; }); }
// 计算测量结果并返回标签文本和中心点 function calculateMeasurement(positions) { if (positions.length < 2) return null;
const coords = cartesiansToTurfCoords(positions); let labelText = ""; let centerCoords = null;
if (drawMode.value === "line") { const line = turf.lineString(coords); const lengthM = turf.length(line) * 1000; // 转换为米
if (lengthM < 1000) { labelText = `距离: ${lengthM.toFixed(2)} 米`; } else { labelText = `距离: ${(lengthM / 1000).toFixed(2)} 公里`; }
centerCoords = turf.center(line).geometry.coordinates; } else if (drawMode.value === "polygon" && positions.length >= 3) { // Turf要求多边形首尾闭合 const closedCoords = [...coords, coords[0]]; const polygon = turf.polygon([closedCoords]); const areaSqm = turf.area(polygon);
if (areaSqm < 10000) { labelText = `面积: ${areaSqm.toFixed(2)} ㎡`; } else if (areaSqm < 1000000) { labelText = `面积: ${(areaSqm / 10000).toFixed(2)} 公顷`; } else { labelText = `面积: ${(areaSqm / 1000000).toFixed(2)} km²`; }
centerCoords = turf.center(polygon).geometry.coordinates; }
if (labelText && centerCoords) { return { text: labelText, center: Cartesian3.fromDegrees(centerCoords[0], centerCoords[1]), }; }
return null; }
// 创建标签实体 function createLabel(position, text) { return viewer.entities.add({ position: position, label: { text: text, font: "bold 14pt sans-serif", fillColor: Color.BLACK, outlineColor: Color.WHITE, outlineWidth: 3, style: LabelStyle.FILL_AND_OUTLINE, pixelOffset: new Cartesian2(0, 0), horizontalOrigin: HorizontalOrigin.CENTER, verticalOrigin: VerticalOrigin.CENTER, disableDepthTestDistance: Number.POSITIVE_INFINITY, }, }); }
// 更新临时预览(图形+标签) function updatePreview(positions) { // 清除旧的临时预览 if (tempShapeEntity) { viewer.entities.remove(tempShapeEntity); tempShapeEntity = null; } if (tempLabelEntity) { viewer.entities.remove(tempLabelEntity); tempLabelEntity = null; }
if (positions.length < 2) return;
// 创建临时图形 if (drawMode.value === "line") { tempShapeEntity = viewer.entities.add({ polyline: { positions: positions, width: 3, material: Color.BLUE, clampToGround: true, }, }); } else if (drawMode.value === "polygon") { if (positions.length >= 3) { tempShapeEntity = viewer.entities.add({ polygon: { hierarchy: new PolygonHierarchy(positions), material: Color.GREEN.withAlpha(0.5), outline: true, outlineColor: Color.BLACK, outlineWidth: 2, heightReference: HeightReference.CLAMP_TO_GROUND, }, }); } else { // 不够3个点时先显示线 tempShapeEntity = viewer.entities.add({ polyline: { positions: positions, width: 3, material: Color.BLUE, clampToGround: true, }, }); } }
// 创建临时标签 const measurement = calculateMeasurement(positions); if (measurement) { tempLabelEntity = createLabel(measurement.center, measurement.text); } }
// 处理点确认 function handlePointConfirmed(cartesian, longitude, latitude) { if (drawMode.value === "point") { // 点直接创建永久实体 viewer.entities.add({ position: cartesian, point: { pixelSize: 12, color: Color.RED, outlineColor: Color.WHITE, outlineWidth: 2, }, label: { text: `经度: ${longitude.toFixed(4)}\n纬度: ${latitude.toFixed(4)}`, font: "12pt sans-serif", fillColor: Color.WHITE, outlineColor: Color.BLACK, outlineWidth: 2, pixelOffset: new Cartesian2(0, -20), disableDepthTestDistance: Number.POSITIVE_INFINITY, }, }); } else { // 线和面:添加到确认点数组 confirmedPoints.value.push(cartesian);
// 点击后立即更新预览 if (confirmedPoints.value.length >= 2) { updatePreview(confirmedPoints.value); } } }
// 完成绘制:将临时实体转为永久实体 function finishDrawing() { // 1. 清除临时预览 if (tempShapeEntity) { viewer.entities.remove(tempShapeEntity); tempShapeEntity = null; } if (tempLabelEntity) { viewer.entities.remove(tempLabelEntity); tempLabelEntity = null; }
// 2. 创建永久的图形和标签 const positions = confirmedPoints.value;
if (drawMode.value === "line" && positions.length >= 2) { // 创建永久的线 viewer.entities.add({ polyline: { positions: positions, width: 3, material: Color.BLUE, clampToGround: true, }, });
// 创建永久的距离标签 const measurement = calculateMeasurement(positions); if (measurement) { createLabel(measurement.center, measurement.text); } } else if (drawMode.value === "polygon" && positions.length >= 3) { // 创建永久的面 viewer.entities.add({ polygon: { hierarchy: new PolygonHierarchy(positions), material: Color.GREEN.withAlpha(0.5), outline: true, outlineColor: Color.BLACK, outlineWidth: 2, heightReference: HeightReference.CLAMP_TO_GROUND, }, });
// 创建永久的面积标签 const measurement = calculateMeasurement(positions); if (measurement) { createLabel(measurement.center, measurement.text); } }
// 3. 重置状态 confirmedPoints.value = []; }
// 设置绘制模式 function setDrawMode(mode) { // 如果正在绘制中,先完成当前绘制 if (drawMode.value !== "none" && confirmedPoints.value.length > 0) { finishDrawing(); }
// 清除所有临时预览 if (tempShapeEntity) { viewer.entities.remove(tempShapeEntity); tempShapeEntity = null; } if (tempLabelEntity) { viewer.entities.remove(tempLabelEntity); tempLabelEntity = null; }
drawMode.value = mode; confirmedPoints.value = []; }
// 清除所有绘制 function clearDrawings() { viewer.entities.removeAll(); confirmedPoints.value = []; tempShapeEntity = null; tempLabelEntity = null; currentPosition.value = null; } </script>
<style scoped> .cesium-container { position: relative; width: 100%; height: 100vh; }
.cesium-map { width: 100%; height: 100%; }
.toolbar { position: absolute; top: 20px; left: 20px; z-index: 1000; display: flex; gap: 10px; padding: 10px; background: rgba(255, 255, 255, 0.9); border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2); }
.toolbar button { padding: 8px 16px; border: 1px solid #ddd; background: white; cursor: pointer; border-radius: 4px; transition: all 0.3s; }
.toolbar button:hover { background: #f0f0f0; }
.toolbar button.active { background: #007bff; color: white; border-color: #007bff; }
.info-panel { position: absolute; bottom: 20px; right: 20px; z-index: 1000; padding: 15px; background: rgba(255, 255, 255, 0.95); border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2); min-width: 200px; }
.info-panel div { margin: 5px 0; font-size: 14px; } </style>
|