topographicMap.vue 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. <template>
  2. <div class="mapBox">
  3. <div id="cesiumContainer" style="width: 100%; height: 100vh"></div>
  4. <div :class="!menuComTStyB ? 'menuComTSty' : 'menuComT'" v-if="0">
  5. <menuCom
  6. :showbasemap="false"
  7. :showwindspeed="false"
  8. :showcloud="false"
  9. :showrainfall="false"
  10. :showtemperature="false"
  11. :showcity="false"
  12. :showwind="false"
  13. :showexit="true"
  14. :showwindModel="true"
  15. @handleWindModel="showwindmodel"
  16. @handleInit="handleInitView"
  17. @handleExit="switchLayer"
  18. />
  19. </div>
  20. <el-drawer
  21. v-model="windDrawer"
  22. direction="rtl"
  23. class="windModelDrawer"
  24. :before-close="handleClose"
  25. >
  26. <template #header>
  27. <h3 style="font-weight: bold">{{ windDrawerHeader }}</h3>
  28. </template>
  29. <template #default>
  30. <div class="windDrawerCla">
  31. <div class="line" v-if="!showModelMsg">
  32. <div class="leftContent">
  33. <span>{{ windDrawerTitle }}</span>
  34. </div>
  35. </div>
  36. <div class="jcxx" v-if="showBasicMsg">
  37. <windHome :modelValItem="modelVal" />
  38. </div>
  39. <div class="spjk" v-if="showVideoMsg">
  40. <!-- <iframe
  41. src="/public/static/windVideo.mp4"
  42. frameborder="0"
  43. style="width: 100%; height: 100%"
  44. ></iframe> -->
  45. <video ref="videoPlayer" controls width="95%">
  46. <source src="/public/static/windVideo.mp4" type="video/mp4" />
  47. </video>
  48. </div>
  49. <div class="gzck" v-if="showProblemMsg">
  50. <windPro />
  51. </div>
  52. <div class="third" v-if="showModelMsg">
  53. <ModelUnpack
  54. :modelUnpackType="modelUnpackType"
  55. :showModelMsg="showModelMsg"
  56. />
  57. </div>
  58. </div>
  59. </template>
  60. </el-drawer>
  61. <comModelDialog
  62. :showcomModelDia="showcomModelDia"
  63. :modelVal="modelVal"
  64. @showDia="showComDia"
  65. />
  66. <windView v-if="showWindDetail" @showDetail="menuComTSty" @backStations="backStations" />
  67. </div>
  68. </template>
  69. <script>
  70. import * as Cesium from "../../Cesium";
  71. import "../../Cesium/Widgets/widgets.css";
  72. import fjLonLatJson from "./fjLonLat.json";
  73. import allStationJson from "./allStationJson.json"
  74. import basicGeoJson from "../../assets/geoJson/basic.json";
  75. import comModelDialog from "@/components/comModelDialog.vue";
  76. import windView from "./windView.vue";
  77. import menuCom from "../menuCom.vue";
  78. // import bw from "@/assets/windimgs/fanSvg/bw.svg"
  79. //风场展示图标
  80. import fc from "@/assets/windimgs/fanSvg/fc.png"
  81. //火电展示图标
  82. import hd from "@/assets/windimgs/fanSvg/hd.png"
  83. //光伏电站展示图标
  84. import gf from "@/assets/windimgs/fanSvg/gf.png"
  85. //故障
  86. import gz from "@/assets/windimgs/fanSvg/gz.svg"
  87. //待机
  88. import dj from "@/assets/windimgs/fanSvg/dj.svg"
  89. //检修
  90. import jx from "@/assets/windimgs/fanSvg/jx.svg"
  91. //限电
  92. import xd from "@/assets/windimgs/fanSvg/xd.svg"
  93. //离线
  94. import lx from "@/assets/windimgs/fanSvg/lx.svg"
  95. //受累
  96. import sl from "@/assets/windimgs/fanSvg/sl.svg"
  97. //动图使用柱子和扇叶
  98. import bwzhu from "@/assets/windimgs/fanSvg/bwzhu.svg"
  99. import bwshan from "@/assets/windimgs/fanSvg/bwshan.png"
  100. import windHome from "@/components/windHome/index.vue";
  101. import windPro from "@/components/windProDetail/windProblem.vue";
  102. import ModelUnpack from "@/components/modelUnpack.vue";
  103. export default {
  104. components: {
  105. comModelDialog,
  106. windView,
  107. menuCom,
  108. windHome,
  109. windPro,
  110. ModelUnpack,
  111. },
  112. data() {
  113. return {
  114. viewer: null,
  115. showcomModelDia: false,
  116. modelVal: null,
  117. menuComTStyB: false,
  118. modelUnpackType: "fengji",
  119. windDrawer: false,
  120. windDrawerTitle: "",
  121. windDrawerHeader: "",
  122. showBasicMsg: false,
  123. showVideoMsg: false,
  124. showProblemMsg: false,
  125. showModelMsg: false,
  126. showWindDetail: true,
  127. allStationentitys: [],
  128. allWindEntitys: []
  129. };
  130. },
  131. mounted() {
  132. this.initCesium();
  133. },
  134. methods: {
  135. async initCesium() {
  136. const box = document.getElementById("cesiumContainer");
  137. const viewer = new Cesium.Viewer(box, {
  138. geocoder: false, // 地址搜索控件
  139. homeButton: false, // 返回地图初始位置控件
  140. infoBox: false, // 地图默认的信息控件
  141. sceneModePicker: false, // 场景模式切换控件
  142. baseLayerPicker: false, // 底图切换控件
  143. navigationHelpButton: false, // 帮助控件
  144. animation: false, // 动画控制控件
  145. timeline: false, // 时间线控件
  146. fullscreenButton: false, // 全屏按钮控件
  147. // imageryProvider: true, // 是否显示 Cesium 默认地图的底图
  148. vrButton: false,
  149. selectionIndicator: false,
  150. shouldAnimate: true,
  151. });
  152. const imageryProvider = new Cesium.UrlTemplateImageryProvider({
  153. url: "https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}",
  154. // url: "http://192.168.2.180:3007/tiles/map/{z}/{x}/{y}",
  155. credit: "影像地图",
  156. });
  157. imageryProvider.alpha = 0.55; // 透明度
  158. imageryProvider.brightness = 1; // 亮度
  159. imageryProvider.contrast = 1; // 对比度
  160. viewer.imageryLayers.addImageryProvider(imageryProvider);
  161. viewer._cesiumWidget._creditContainer.style.display = "none";
  162. // this.csceneElliposid(viewer)
  163. this.viewer = viewer;
  164. // 展示场站
  165. // this.showAllStation(viewer)
  166. // 展示风机
  167. this.showWindFromStation(viewer)
  168. this.initGeoJsonData()
  169. },
  170. //限制相机在区域内活动
  171. allowedRectangleFn(viewer) {
  172. // 定义允许的区域范围(WGS84 坐标,单位:度)
  173. const allowedRectangle = Cesium.Rectangle.fromDegrees(
  174. 113.9, // west
  175. 34.8, // south
  176. 115, // east
  177. 35.7 // north
  178. );
  179. // viewer.scene.preUpdate.addEventListener(function(scene, time) {
  180. // const camera = viewer.camera;
  181. // const position = camera.positionCartographic;
  182. // if (position) {
  183. // const lon = Cesium.Math.toDegrees(position.longitude);
  184. // const lat = Cesium.Math.toDegrees(position.latitude);
  185. // // 判断是否越界
  186. // if (lon < allowedRectangle.west || lon > allowedRectangle.east ||
  187. // lat < allowedRectangle.south || lat > allowedRectangle.north) {
  188. // // 获取当前视角方向,用于保持高度和朝向
  189. // const height = position.height;
  190. // let targetLon = Cesium.Math.clamp(lon, allowedRectangle.west, allowedRectangle.east);
  191. // let targetLat = Cesium.Math.clamp(lat, allowedRectangle.south, allowedRectangle.north);
  192. // // camera.flyTo({
  193. // // destination: Cesium.Cartesian3.fromDegrees(targetLon, targetLat, height),
  194. // // duration: 0.1, // 可以设置轻微动画,更自然
  195. // // maximumHeight: height,
  196. // // complete: function() {
  197. // // // 防止连续触发
  198. // // }
  199. // // });
  200. // }
  201. // }
  202. // });
  203. viewer.camera.changed.addEventListener(function() {
  204. const cameraPositionCartographic = viewer.camera.positionCartographic;
  205. // 检查相机是否在允许的区域内
  206. if (cameraPositionCartographic) {
  207. const lon = Cesium.Math.toDegrees(cameraPositionCartographic.longitude);
  208. const lat = Cesium.Math.toDegrees(cameraPositionCartographic.latitude);
  209. let corrected = false;
  210. let correctedLon = lon;
  211. let correctedLat = lat;
  212. // 限制经度
  213. if (lon < allowedRectangle.west) {
  214. correctedLon = allowedRectangle.west;
  215. corrected = true;
  216. } else if (lon > allowedRectangle.east) {
  217. correctedLon = allowedRectangle.east;
  218. corrected = true;
  219. }
  220. // 限制纬度
  221. if (lat < allowedRectangle.south) {
  222. correctedLat = allowedRectangle.south;
  223. corrected = true;
  224. } else if (lat > allowedRectangle.north) {
  225. correctedLat = allowedRectangle.north;
  226. corrected = true;
  227. }
  228. // 如果超出范围,将相机位置纠正
  229. if (corrected) {
  230. viewer.camera.flyTo({
  231. destination: Cesium.Cartesian3.fromDegrees(correctedLon, correctedLat, cameraPositionCartographic.height),
  232. duration: 0 // 立即跳转,无动画
  233. });
  234. }
  235. }
  236. })
  237. },
  238. // 初始化 geoJson 数据
  239. async initGeoJsonData() {
  240. // 创建GeoJSON数据源
  241. await new Cesium.GeoJsonDataSource.load(basicGeoJson, {
  242. stroke: Cesium.Color.GRAY, // 边界线颜色
  243. fill: Cesium.Color.BLACK.withAlpha(0), // 填充颜色
  244. strokeWidth: 1, // 边界线宽度
  245. markerSymbol: "?", // 点要素的符号
  246. clampToGround: true, // 贴地
  247. }).then((dataSource) => {
  248. // 添加到视图
  249. this.viewer.dataSources.add(dataSource);
  250. var entities = dataSource.entities.values;
  251. for (let i = 0; i < entities.length; i++) {
  252. let entity = entities[i];
  253. entity.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions;
  254. //单独设置线条样式
  255. var positions = entity.polygon.hierarchy._value.positions;
  256. entity.polyline = {
  257. positions: positions,
  258. width: 1,
  259. outline: false,
  260. };
  261. }
  262. // 添加中文标签图层
  263. const labelLayer = new Cesium.LabelCollection();
  264. this.viewer.scene.primitives.add(labelLayer);
  265. const cities = [];
  266. basicGeoJson?.features?.forEach((ele) => {
  267. if (Array.isArray(ele.properties.centroid)) {
  268. const name = ele.properties.name;
  269. const lon = ele.properties.centroid[0];
  270. const lat = ele.properties.centroid[1];
  271. cities.push({ name, lon, lat });
  272. }
  273. });
  274. cities.forEach((city, index) => {
  275. labelLayer.add({
  276. id: index,
  277. name: "cityLabel",
  278. position: Cesium.Cartesian3.fromDegrees(city.lon, city.lat, 10),
  279. text: city.name,
  280. font: 'bold 14px "Microsoft YaHei", sans-serif',
  281. fillColor: Cesium.Color.fromCssColorString("#000"),
  282. outlineColor: Cesium.Color.WHITE,
  283. outlineWidth: 2,
  284. style: Cesium.LabelStyle.FILL_AND_OUTLINE,
  285. pixelOffset: new Cesium.Cartesian2(0, 0), // 设置为0
  286. horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平居中
  287. verticalOrigin: Cesium.VerticalOrigin.CENTER, // 垂直居中
  288. });
  289. });
  290. });
  291. },
  292. backStations() {
  293. this.showWindDetail = false
  294. this.allWindEntitys.forEach(({ entity, handler }) => {
  295. this.viewer.entities.remove(entity); // 移除实体
  296. if (!handler.isDestroyed()) {
  297. handler.destroy(); // 销毁事件处理器(关键!)
  298. }
  299. })
  300. this.allWindEntitys = []
  301. // if (this.viewer && this.viewer.destroy) {
  302. // this.viewer.destroy();
  303. // this.viewer = null
  304. // }
  305. // this.initCesium();
  306. this.showAllStation(this.viewer)
  307. },
  308. // 展示所有风场
  309. showAllStation(viewer) {
  310. allStationJson.station.forEach((e, index) => {
  311. if (e.energytype === "Wind") {
  312. this.showStationFn(viewer, e, index, fc)
  313. } else if(e.energytype === "Fire") {
  314. this.showStationFn(viewer, e, index, hd)
  315. } else {
  316. this.showStationFn(viewer, e, index, gf)
  317. }
  318. });
  319. this.resetAllStationViewport()
  320. },
  321. // 根据状态展示不同颜色风机贴图
  322. showStationFn(viewer, e, index, images) {
  323. const position = Cesium.Cartesian3.fromDegrees(e.longitude, e.latitude);
  324. const entity = viewer.entities.add({
  325. id: index,
  326. position, // 模型位置
  327. billboard: {
  328. image: images, // 也可以是 SVG 路径,如 'icon.svg'
  329. scale: 0.5,
  330. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  331. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  332. // 模型贴地
  333. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  334. },
  335. label: {
  336. text: e.plantname,
  337. font: '14px sans-serif',
  338. fillColor: Cesium.Color.fromBytes(255, 255, 255),
  339. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  340. }
  341. });
  342. let that = this
  343. const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  344. handler.setInputAction(function (movement) {
  345. var position = movement.position;
  346. var pickedObject = viewer.scene.pick(position);
  347. if (pickedObject && pickedObject.id.id === index) {
  348. console.log("你点击了标签或模型!", entity);
  349. console.log("选中风场或新能源场", e.plantname);
  350. that.showWindDetail = true
  351. that.allStationentitys.forEach(({ entity, handler }) => {
  352. viewer.entities.remove(entity); // 移除实体
  353. if (!handler.isDestroyed()) {
  354. handler.destroy(); // 销毁事件处理器(关键!)
  355. }
  356. })
  357. that.allStationentitys = []
  358. that.showWindFromStation(viewer)
  359. }
  360. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  361. that.allStationentitys.push({entity, handler})
  362. },
  363. // 展示所选风场的风机
  364. showWindFromStation(viewer) {
  365. fjLonLatJson.data.forEach((e, index) => {
  366. if (e.status) {
  367. if (e.status === 1) {
  368. this.showStatuswind(viewer, e, dj)
  369. } else if (e.status === 2) {
  370. this.showStatuswind(viewer, e, gz)
  371. } else if (e.status === 3) {
  372. this.showStatuswind(viewer, e, jx)
  373. } else if (e.status === 4) {
  374. this.showStatuswind(viewer, e, xd)
  375. } else if (e.status === 5) {
  376. this.showStatuswind(viewer, e, lx)
  377. } else if (e.status === 6) {
  378. this.showStatuswind(viewer, e, sl)
  379. } else {
  380. //并网风机
  381. this.showAnimatewind(viewer, e)
  382. }
  383. }
  384. });
  385. this.resetWindViewport()
  386. },
  387. // 根据状态展示不同颜色风机贴图
  388. showStatuswind(viewer, e, type, index) {
  389. this.addSvg(
  390. viewer,
  391. type,
  392. e.longitude,
  393. e.latitude,
  394. e,
  395. Math.random().toFixed(4)*10000
  396. );
  397. },
  398. // 风机柱与扇叶分开展示转动效果贴图
  399. showAnimatewind(viewer, e, index) {
  400. this.addSvgs(
  401. viewer,
  402. bwshan,
  403. e.longitude,
  404. e.latitude,
  405. e,
  406. Math.random().toFixed(3)*1000
  407. );
  408. this.addSvg(
  409. viewer,
  410. bwzhu,
  411. e.longitude,
  412. e.latitude,
  413. e,
  414. Math.random().toFixed(3)*2000
  415. );
  416. },
  417. // 生成随机数
  418. randomNum(minNum, maxNum) {
  419. switch (arguments.length) {
  420. case 1:
  421. return parseInt(Math.random() * minNum + 1, 10);
  422. case 2:
  423. return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
  424. default:
  425. return 0;
  426. }
  427. },
  428. //给模型上描边
  429. getWtStatue(code) {
  430. if (code === 1) {
  431. return "#fff";
  432. } else if (code === 2) {
  433. return "#1890ff";
  434. } else {
  435. return "#f25656";
  436. }
  437. },
  438. //添加模型
  439. addModel(viewer, uri, name, lon, lat, scale, val) {
  440. const hpRoll = new Cesium.HeadingPitchRoll(90.0, 0.0, 0.0);
  441. const position = Cesium.Cartesian3.fromDegrees(lon, lat);
  442. const orientation = Cesium.Transforms.headingPitchRollQuaternion(
  443. position,
  444. hpRoll
  445. );
  446. const wtStatue = this.randomNum(1, 3);
  447. const statueColor = this.getWtStatue(wtStatue);
  448. const entity = viewer.entities.add({
  449. name, // 模型名称
  450. position, // 模型位置
  451. orientation, // 模型朝向
  452. animation: false,
  453. model: {
  454. uri,
  455. scale,
  456. // 模型贴地
  457. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  458. silhouetteSize: 2,
  459. silhouetteColor: Cesium.Color.fromCssColorString(statueColor),
  460. runAnimations: wtStatue === 3 ? false : true,
  461. },
  462. //添加标签
  463. label: {
  464. text: val ? val.name : name, // 标签文字
  465. font: "14px sans-serif", // 字体
  466. fillColor: Cesium.Color.fromBytes(255, 255, 255), // 填充颜色
  467. outlineColor: Cesium.Color.BLACK, // 描边颜色
  468. outlineWidth: 2, // 描边宽度
  469. style: Cesium.LabelStyle.FILL_AND_OUTLINE, // 填充+描边
  470. verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // 文本在模型上方
  471. horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平居中
  472. eyeOffset: new Cesium.Cartesian3(-100, 1100, 5), // 向上 15 米
  473. translucencyByDistance: new Cesium.NearFarScalar(
  474. 1.0e6,
  475. 1.0,
  476. 6.0e6,
  477. 0.0
  478. ),
  479. },
  480. });
  481. let that = this;
  482. // 创建事件处理器
  483. const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  484. // handler.setInputAction(function(click) {
  485. // const picked = viewer.scene.pick(click.position);
  486. // if (picked && picked.id === entity) {
  487. // console.log('你点击了标签或模型!', entity);
  488. // console.log('标签或模型数据!', val);
  489. // // alert('你点击了: ' + entity.label.text.getValue());
  490. // // this.$refs.comModelDialog.init(val)
  491. // if (name !== '光伏') {
  492. // that.showcomModelDia = true
  493. // that.modelVal = val
  494. // }
  495. // }
  496. // }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  497. handler.setInputAction(function (movement) {
  498. var position = movement.position;
  499. var pickedObject = viewer.scene.pick(position);
  500. if (pickedObject && pickedObject.id === entity) {
  501. console.log("你点击了标签或模型!", entity);
  502. console.log("标签或模型数据!", val);
  503. that.modelVal = val;
  504. // 找到实体,显示包含实体信息的弹框
  505. that.showRightClickPopup(position, val);
  506. return;
  507. }
  508. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  509. },
  510. //添加svg或png贴图
  511. addSvg(viewer, uri, lon, lat, val, index) {
  512. let that = this;
  513. let ids = that.generateUniqueId(val.id)
  514. const position = Cesium.Cartesian3.fromDegrees(lon, lat);
  515. const entity = viewer.entities.add({
  516. id: ids,
  517. position, // 模型位置
  518. billboard: {
  519. image: uri, // 也可以是 SVG 路径,如 'icon.svg'
  520. scale: 0.5,
  521. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  522. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  523. // 模型贴地
  524. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  525. },
  526. label: {
  527. text: val.name ? val.name : val.plantname,
  528. font: '14px sans-serif',
  529. fillColor: Cesium.Color.fromBytes(255, 255, 255),
  530. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  531. pixelOffset: val.name === 'F10号风机' ? new Cesium.Cartesian2(40, -20) : new Cesium.Cartesian2(0, 0)
  532. }
  533. });
  534. // 创建事件处理器
  535. const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  536. handler.setInputAction(function (movement) {
  537. var position = movement.position;
  538. var pickedObject = viewer.scene.pick(position);
  539. if (pickedObject && pickedObject.id.id === ids) {
  540. console.log("你点击了标签或模型!", entity);
  541. console.log("标签或模型数据!", val);
  542. that.modelVal = val;
  543. // 找到实体,显示包含实体信息的弹框
  544. that.showRightClickPopup(position, val);
  545. return;
  546. }
  547. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  548. this.allWindEntitys.push({entity, handler})
  549. },
  550. //添加svg或png贴图
  551. addSvgs(viewer, uri, lon, lat, val, index) {
  552. let that = this;
  553. let ids = that.generateUniqueId(val.id)
  554. const position = Cesium.Cartesian3.fromDegrees(lon, lat);
  555. const entity = viewer.entities.add({
  556. id: ids,
  557. position, // 模型位置
  558. billboard: {
  559. image: uri, // 也可以是 SVG 路径,如 'icon.svg'
  560. scale: 0.5,
  561. pixelOffset: new Cesium.Cartesian2(0, -15),
  562. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  563. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  564. // 模型贴地
  565. heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
  566. // 核心:使用 CallbackProperty 实现旋转动画
  567. rotation: new Cesium.CallbackProperty(function(time) {
  568. // 每 3 秒转一圈(可调速度)
  569. const seconds = time.secondsOfDay;
  570. const angle = Cesium.Math.toRadians((seconds % 3) * -360 / 3); // 每3秒一圈
  571. return angle;
  572. }, false),
  573. // 确保绕中心旋转
  574. rotationAlignment: Cesium.HeightReference.CENTER,
  575. alignedAxis: Cesium.Cartesian3.UNIT_Z
  576. }
  577. });
  578. // 创建事件处理器
  579. const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  580. handler.setInputAction(function (movement) {
  581. var position = movement.position;
  582. var pickedObject = viewer.scene.pick(position);
  583. if (pickedObject && pickedObject.id.id === ids) {
  584. console.log("你点击了标签或模型!", entity);
  585. console.log("标签或模型数据!", val);
  586. that.modelVal = val;
  587. // 找到实体,显示包含实体信息的弹框
  588. that.showRightClickPopup(position, val);
  589. return;
  590. }
  591. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
  592. this.allWindEntitys.push({entity, handler})
  593. },
  594. // 右键展示元素
  595. showRightClickPopup(screenPosition, val) {
  596. // 创建或获取弹框元素
  597. var popup = document.getElementById("rightClickPopup");
  598. if (!popup) {
  599. popup = document.createElement("div");
  600. popup.id = "rightClickPopup";
  601. popup.style.position = "absolute";
  602. // popup.style.backgroundColor = 'white';
  603. popup.style.border = "1px solid rgba(255, 255, 255, 0.2)";
  604. popup.style.borderRadius = "4px";
  605. popup.style.padding = "10px";
  606. popup.style.boxShadow = "0 2px 10px rgba(0,0,0,0.2)";
  607. popup.style.backdropFilter = `blur(10px)`;
  608. popup.style.zIndex = "1000";
  609. popup.style.pointerEvents = "auto"; // 确保弹框可交互
  610. // 添加一个最小宽度,避免内容过短
  611. popup.style.minWidth = "100px";
  612. // 可选:添加箭头指向点击点
  613. // 这需要更复杂的 CSS 或额外的 DOM 元素
  614. document.body.appendChild(popup);
  615. }
  616. // <span class="popup-menu-item" data-message-type="model" style="cursor:pointer">模型解构</span>
  617. let content = `
  618. <div style="display: flex;gap: 10px;flex-direction: column;text-align: center;color: #fff">
  619. <span class="popup-menu-item" data-message-type="basic" style="cursor:pointer">基本信息</span>
  620. <span class="popup-menu-item" data-message-type="video" style="cursor:pointer">视频监控</span>
  621. <span class="popup-menu-item" data-message-type="problem" style="cursor:pointer">故障详情</span>
  622. </div>
  623. `;
  624. // 设置弹框内容
  625. popup.innerHTML = content;
  626. // --- 关键修正:准确计算弹框位置 ---
  627. // 1. 获取 Cesium 画布 (Canvas) 相对于视口 (viewport) 的位置
  628. var canvas = this.viewer.scene.canvas;
  629. var canvasRect = canvas.getBoundingClientRect();
  630. // console.log('Canvas Rect:', canvasRect); // 调试用
  631. // 2. 计算弹框左上角在页面中的绝对 X 坐标
  632. // screenPosition.x 是相对于画布左上角的 X 偏移
  633. // canvasRect.left 是画布左上角相对于浏览器视口左上角的 X 偏移
  634. // window.pageXOffset 是页面水平滚动的距离
  635. var popupLeft = canvasRect.left + screenPosition.x + window.pageXOffset;
  636. // 3. 计算弹框左上角在页面中的绝对 Y 坐标
  637. var popupTop = canvasRect.top + screenPosition.y + window.pageYOffset;
  638. // --- 可选:添加偏移量,避免完全覆盖鼠标指针 ---
  639. // 例如,向下和向右偏移 10px
  640. var offsetX = 10;
  641. var offsetY = 10;
  642. popupLeft += offsetX;
  643. popupTop += offsetY;
  644. // --- 可选:边界检查,防止弹框超出屏幕 ---
  645. var popupWidth = popup.offsetWidth || 150; // 确保有宽度
  646. var popupHeight = popup.offsetHeight || 50; // 确保有高度
  647. var windowWidth = window.innerWidth;
  648. var windowHeight = window.innerHeight;
  649. // 如果弹框右边会超出窗口,则左移
  650. if (popupLeft + popupWidth > windowWidth) {
  651. popupLeft = windowWidth - popupWidth - 10; // 靠右留 10px 边距
  652. }
  653. // 如果弹框底边会超出窗口,则上移
  654. if (popupTop + popupHeight > windowHeight) {
  655. popupTop = windowHeight - popupHeight - 10; // 靠下留 10px 边距
  656. }
  657. // 如果左边超出 (不太可能,但安全起见)
  658. if (popupLeft < 0) popupLeft = 10;
  659. // 如果顶边超出
  660. if (popupTop < 0) popupTop = 10;
  661. // --- 设置最终位置 ---
  662. popup.style.left = popupLeft + "px";
  663. popup.style.top = popupTop + "px";
  664. // console.log('Popup Position:', { left: popupLeft, top: popupTop }); // 调试用
  665. // 显示弹框
  666. popup.style.display = "block";
  667. // --- 隐藏弹框的逻辑 (保持不变) ---
  668. function hidePopup(event) {
  669. // 检查点击是否发生在弹框内部,如果是,则不隐藏
  670. if (event && popup.contains(event.target)) {
  671. return;
  672. }
  673. popup.style.display = "none";
  674. document.removeEventListener("click", hidePopup);
  675. document.removeEventListener("keydown", keyDownHandler);
  676. }
  677. function keyDownHandler(event) {
  678. if (event.key === "Escape") {
  679. hidePopup();
  680. }
  681. }
  682. let that = this;
  683. that.showBasicMsg = false;
  684. that.showVideoMsg = false;
  685. that.showProblemMsg = false;
  686. that.showModelMsg = false;
  687. function popupMenuItemClick(event) {
  688. // 检查点击的是否是菜单项
  689. if (event.target.classList.contains("popup-menu-item")) {
  690. event.stopPropagation(); // 阻止冒泡,防止弹框立即关闭
  691. var messageType = event.target.dataset.messageType; // 获取 data-message-type
  692. console.log("点击了菜单项:", messageType);
  693. // 调用您的 showMessage 函数
  694. that.showMessage(messageType, val);
  695. hidePopup();
  696. // 可选:执行后关闭弹框
  697. // hideRightClickPopup();
  698. }
  699. }
  700. // 移除旧的监听器(避免重复绑定)
  701. document.removeEventListener("click", hidePopup);
  702. document.removeEventListener("click", popupMenuItemClick);
  703. document.removeEventListener("keydown", keyDownHandler);
  704. // 添加新的监听器
  705. document.addEventListener("click", hidePopup);
  706. document.addEventListener("click", popupMenuItemClick);
  707. document.addEventListener("keydown", keyDownHandler);
  708. },
  709. showMessage(type, val) {
  710. console.log("type===>>>", type);
  711. this.windDrawer = true;
  712. this.windDrawerHeader = val.name + "数据详情";
  713. if (type === "basic") {
  714. this.windDrawerTitle = "基础信息";
  715. this.showBasicMsg = true;
  716. } else if (type === "video") {
  717. this.windDrawerTitle = "视频监控";
  718. this.showVideoMsg = true;
  719. } else if (type === "problem") {
  720. this.windDrawerTitle = "故障查看";
  721. this.showProblemMsg = true;
  722. }
  723. // else {
  724. // this.windDrawerTitle = '模型解构'
  725. // this.showModelMsg = true
  726. // }
  727. },
  728. showwindmodel() {
  729. this.windDrawer = true;
  730. this.showBasicMsg = false;
  731. this.showVideoMsg = false;
  732. this.showProblemMsg = false;
  733. this.windDrawerHeader = "风机模型可视化解构说明";
  734. this.showModelMsg = true;
  735. },
  736. handleClose() {
  737. this.windDrawer = false;
  738. this.showBasicMsg = false;
  739. this.showVideoMsg = false;
  740. this.showProblemMsg = false;
  741. this.showModelMsg = false;
  742. },
  743. showComDia(val) {
  744. this.showcomModelDia = val;
  745. this.modelVal = null;
  746. },
  747. csceneElliposid(viewer) {
  748. let that = this;
  749. // 获取 scene 和 ellipsoid
  750. var scene = viewer.scene;
  751. var labels = viewer.scene.primitives.add(new Cesium.LabelCollection());
  752. // 创建 ScreenSpaceEventHandler
  753. var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  754. // 监听左键点击事件
  755. handler.setInputAction(async (click) => {
  756. // 获取点击位置的笛卡尔坐标
  757. var position = click.position;
  758. if (!position) return;
  759. // 使用 globe.pick 获取包含地形高度的坐标
  760. var ray = viewer.camera.getPickRay(position);
  761. var cartesian = viewer.scene.globe.pick(ray, viewer.scene);
  762. if (cartesian) {
  763. // 转换为地理坐标
  764. var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
  765. var longitude = Cesium.Math.toDegrees(cartographic.longitude);
  766. var latitude = Cesium.Math.toDegrees(cartographic.latitude);
  767. var height = cartographic.height;
  768. // 格式化坐标
  769. var text = `经度: ${longitude.toFixed(6)}°\n纬度: ${latitude.toFixed(
  770. 6
  771. )}°`;
  772. // 创建一个标签
  773. var label = labels.add({
  774. position: cartesian,
  775. text: text,
  776. font: "14px monospace",
  777. fillColor: Cesium.Color.fromCssColorString("#1d70df"),
  778. // style: Cesium.LabelStyle.FILL_AND_OUTLINE,
  779. // outlineColor: Cesium.Color.BLACK,
  780. outlineWidth: 2,
  781. verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // 标签在点的上方
  782. pixelOffset: new Cesium.Cartesian2(0, -20), // 向上偏移一点
  783. disableDepthTest: true, // 让标签始终可见(即使在地球背面)
  784. scale: 0.8,
  785. });
  786. // 5秒后移除标签
  787. setTimeout(function () {
  788. labels.remove(label);
  789. }, 5000);
  790. console.log(
  791. `点击坐标: ${longitude.toFixed(6)}, ${latitude.toFixed(
  792. 6
  793. )}, ${height.toFixed(2)}m`
  794. );
  795. }
  796. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  797. },
  798. generateUniqueId(prefix = 'id') {
  799. let idCounter = 0;
  800. return `${prefix}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}-${idCounter++}`;
  801. },
  802. handleInitView() {
  803. if (this.showWindDetail) {
  804. this.resetWindViewport()
  805. } else {
  806. this.resetAllStationViewport()
  807. }
  808. },
  809. // 重置所有风场视角
  810. resetAllStationViewport() {
  811. this.viewer.camera.flyTo({
  812. destination: Cesium.Cartesian3.fromDegrees(114.502778, 35.326667, 3000000),
  813. orientation: {
  814. heading: Cesium.Math.toRadians(0),
  815. pitch: Cesium.Math.toRadians(-90),
  816. roll: 0.0,
  817. },
  818. duration: 3.0,
  819. });
  820. },
  821. // 重置风场中所有风机视角
  822. resetWindViewport() {
  823. // 目标位置:经度、纬度、高度
  824. const targetLon = 114.502778;
  825. const targetLat = 35.326667;
  826. const targetHeight = 20000;
  827. const draggableHeightTolerance = 10000; // 允许拖拽的高度范围:18,000 ~ 22,000
  828. const minHeight = 10000; // 最低高度
  829. const maxHeight = 100000; // 最高高度
  830. const allowedOffsetDegrees = 0.2; // 允许拖拽的最大偏移(经纬度)
  831. const minLon = targetLon - allowedOffsetDegrees;
  832. const maxLon = targetLon + allowedOffsetDegrees;
  833. const minLat = targetLat - allowedOffsetDegrees;
  834. const maxLat = targetLat + allowedOffsetDegrees;
  835. let that = this
  836. that.viewer.camera.flyTo({
  837. destination: Cesium.Cartesian3.fromDegrees(targetLon,targetLat, targetHeight),
  838. orientation: {
  839. heading: Cesium.Math.toRadians(0),
  840. pitch: Cesium.Math.toRadians(-90),
  841. roll: 0.0,
  842. },
  843. duration: 3.0,
  844. complete: function () {
  845. console.log('飞入完成,启用拖拽限制逻辑');
  846. enableHeightBasedDragControl();
  847. }
  848. });
  849. // ===== 控制逻辑:根据高度决定是否允许拖拽 =====
  850. function enableHeightBasedDragControl() {
  851. that.viewer.scene.preUpdate.addEventListener(function (scene, time) {
  852. const camera = that.viewer.camera;
  853. const posCartographic = camera.positionCartographic;
  854. if (!posCartographic) return;
  855. const currentHeight = posCartographic.height;
  856. const currentLon = Cesium.Math.toDegrees(posCartographic.longitude);
  857. const currentLat = Cesium.Math.toDegrees(posCartographic.latitude);
  858. // === 第一步:限制相机高度不能超出 [10000, 100000] ===
  859. if (currentHeight < minHeight) {
  860. // 强制拉高到 minHeight,但保持当前水平视角
  861. const newPos = Cesium.Cartesian3.fromDegrees(currentLon, currentLat, minHeight);
  862. camera.setView({
  863. destination: newPos,
  864. orientation: {
  865. heading: camera.heading,
  866. pitch: camera.pitch,
  867. roll: camera.roll
  868. }
  869. });
  870. return; // 避免后续逻辑冲突
  871. }
  872. if (currentHeight > maxHeight) {
  873. const newPos = Cesium.Cartesian3.fromDegrees(currentLon, currentLat, maxHeight);
  874. camera.setView({
  875. destination: newPos,
  876. orientation: {
  877. heading: camera.heading,
  878. pitch: camera.pitch,
  879. roll: camera.roll
  880. }
  881. });
  882. return;
  883. }
  884. // === 第二步:判断是否在“可拖拽高度区间” ===
  885. const isInDraggableRange =
  886. currentHeight >= (targetHeight - draggableHeightTolerance) &&
  887. currentHeight <= (targetHeight + draggableHeightTolerance);
  888. if (isInDraggableRange) {
  889. // ✅ 允许拖拽,但限制在指定范围内
  890. const correctedLon = Cesium.Math.clamp(currentLon, minLon, maxLon);
  891. const correctedLat = Cesium.Math.clamp(currentLat, minLat, maxLat);
  892. if (Math.abs(correctedLon - currentLon) > 1e-8 ||
  893. Math.abs(correctedLat - currentLat) > 1e-8) {
  894. // 越界了,纠正位置,保留当前高度和视角
  895. camera.setView({
  896. destination: Cesium.Cartesian3.fromDegrees(correctedLon, correctedLat, currentHeight),
  897. orientation: {
  898. heading: camera.heading,
  899. pitch: camera.pitch,
  900. roll: camera.roll
  901. }
  902. });
  903. }
  904. } else {
  905. // ❌ 不在可拖拽高度:禁止平移,强制回正到目标点
  906. const correctedPosition = Cesium.Cartesian3.fromDegrees(
  907. targetLon,
  908. targetLat,
  909. currentHeight // 保留当前缩放高度
  910. );
  911. camera.setView({
  912. destination: correctedPosition,
  913. orientation: {
  914. heading: camera.heading,
  915. pitch: camera.pitch,
  916. roll: camera.roll
  917. }
  918. });
  919. }
  920. });
  921. }
  922. },
  923. switchLayer() {
  924. this.$router.push({
  925. path: "/",
  926. });
  927. },
  928. menuComTSty(val) {
  929. this.menuComTStyB = val;
  930. },
  931. },
  932. };
  933. </script>
  934. <style lang="less" scoped>
  935. .mapBox {
  936. width: 100%;
  937. height: 100%;
  938. position: relative;
  939. box-sizing: content-box;
  940. overflow: hidden;
  941. .menuComT {
  942. position: fixed;
  943. bottom: 400px;
  944. left: 20px;
  945. }
  946. .menuComTSty {
  947. position: fixed;
  948. bottom: 20px;
  949. left: 20px;
  950. }
  951. }
  952. </style>
  953. <style lang="less">
  954. .el-overlay {
  955. background-color: transparent !important;
  956. .windModelDrawer {
  957. width: 80% !important;
  958. backdrop-filter: blur(15px) !important;
  959. background: rgba(255, 255, 255, .8) !important;
  960. border-radius: 10px 0 0 10px !important;
  961. .el-drawer__body {
  962. overflow: hidden;
  963. padding-top: 0;
  964. }
  965. }
  966. }
  967. .windDrawerCla {
  968. .line {
  969. display: flex;
  970. flex-direction: row;
  971. align-items: center;
  972. justify-content: space-between;
  973. width: 100%;
  974. margin-bottom: 10px;
  975. .leftContent {
  976. width: 242px;
  977. height: 41px;
  978. display: flex;
  979. align-items: center;
  980. background: url("@/assets/cesiumImg/title_left_bg.png") no-repeat;
  981. span {
  982. font-size: 16px;
  983. font-family: Microsoft YaHei;
  984. font-weight: 400;
  985. color: #ffffff;
  986. margin-left: 25px;
  987. }
  988. }
  989. }
  990. .jcxx,
  991. .gzck {
  992. height: 100%;
  993. }
  994. .spjk,
  995. .third {
  996. height: 80vh;
  997. }
  998. }
  999. </style>