TyphoonViewer.vue 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. <template>
  2. <div id="cesiumContainer" style="height: 100%;width: 100%;"></div>
  3. <!-- 控制按钮 -->
  4. <div class="control-buttons">
  5. <button class="control-btn" @click="goToHomeView" title="返回首页视角">
  6. <i class="fas fa-home"></i>
  7. </button>
  8. <button class="control-btn" @click="togglePOIDisplay" title="显示/隐藏POI点">
  9. <i class="fas fa-map-marker-alt" :class="{ 'active': poiVisible }"></i>
  10. </button>
  11. <button class="control-btn" @click="toggleTyphoon" title="显示/隐藏台风并切换视角">
  12. <i class="fas fa-wind" :class="{ 'active': typhoonVisible }"></i>
  13. </button>
  14. </div>
  15. <!-- 自定义弹框组件 - POI点 -->
  16. <div v-if="selectedPoint" class="custom-popup" :style="{
  17. left: `${popupPosition.x}px`,
  18. top: `${popupPosition.y}px`
  19. }">
  20. <div class="popup-content">
  21. <h3>{{ selectedPoint.STNM || '未知点' }}</h3>
  22. <p><strong>经度:</strong> {{ selectedPoint.LGTD }}</p>
  23. <p><strong>纬度:</strong> {{ selectedPoint.LTTD }}</p>
  24. <div class="popup-arrow"></div>
  25. </div>
  26. </div>
  27. <!-- 台风路径点信息弹窗 -->
  28. <div v-if="typhoonInfoVisible && typhoonVisible" class="typhoon-popup" :style="{
  29. left: `${typhoonPopupPosition.x}px`,
  30. top: `${typhoonPopupPosition.y}px`
  31. }">
  32. <div class="popup-content">
  33. <h3>{{ typhoonInfo.name }}({{ typhoonInfo.enname }})</h3>
  34. <p>{{ typhoonInfo.time }}</p>
  35. <p><strong>中心位置:</strong> {{ typhoonInfo.lng }}° / {{ typhoonInfo.lat }}°</p>
  36. <p><strong>风速风力:</strong> {{ typhoonInfo.speed }} m/s ({{ typhoonInfo.power }}级)</p>
  37. <p><strong>中心气压:</strong> {{ typhoonInfo.pressure }} hPa</p>
  38. <p><strong>移速移向:</strong> {{ typhoonInfo.movedirection }} {{ typhoonInfo.movespeed }} km/h</p>
  39. <p><strong>七级半径:</strong> {{ typhoonInfo.radius7 || '--' }}</p>
  40. <p><strong>十级半径:</strong> {{ typhoonInfo.radius10 || '--' }}</p>
  41. <p><strong>十二级半径:</strong> {{ typhoonInfo.radius12 || '--' }}</p>
  42. <div class="popup-arrow"></div>
  43. </div>
  44. </div>
  45. <!-- 台风图例面板 - 添加v-if控制显示 -->
  46. <ul class="legend" v-if="typhoonVisible">
  47. <li>
  48. <span class="dot green"></span>
  49. <span>热带低压</span>
  50. </li>
  51. <li>
  52. <span class="dot blue"></span>
  53. <span>热带风暴</span>
  54. </li>
  55. <li>
  56. <span class="dot yellow"></span>
  57. <span>强热带风暴</span>
  58. </li>
  59. <li>
  60. <span class="dot orange"></span>
  61. <span>台风</span>
  62. </li>
  63. <li>
  64. <span class="dot red"></span>
  65. <span>超强台风</span>
  66. </li>
  67. </ul>
  68. </template>
  69. <script setup>
  70. import { ref, onMounted, onUnmounted } from 'vue'
  71. import * as Cesium from 'cesium';
  72. import "cesium/Build/CesiumUnminified/Widgets/widgets.css";
  73. import JYLData from '@/assets/Data/THJYL.json'
  74. import axios from 'axios'
  75. // 原有变量
  76. const TDTTK = "d9e7aa2ad204aba6aeedea6f5ab48ed9";
  77. const selectedPoint = ref(null);
  78. const popupPosition = ref({ x: 0, y: 0 });
  79. let handler = null;
  80. let viewer = null;
  81. // 新增台风信息弹窗变量
  82. const typhoonInfoVisible = ref(false);
  83. const typhoonInfo = ref({});
  84. const typhoonPopupPosition = ref({ x: 0, y: 0 });
  85. const typhoonPointDataMap = ref(new Map()); // 存储台风点与数据的映射
  86. // 控制按钮相关变量
  87. const poiVisible = ref(true); // POI点显示状态
  88. const typhoonVisible = ref(true); // 台风显示状态
  89. const poiEntities = ref([]); // 存储POI实体,用于控制显示隐藏
  90. // 台风相关变量
  91. const fengquanLayers = ref([]);
  92. const myEntityCollection = ref(null);
  93. const tbentity = ref(null);
  94. const iii = ref(0);
  95. const currentPointObj = ref(null);
  96. let typhoonInterval = null;
  97. // 台风相关实体集合,用于控制显示隐藏
  98. const typhoonRelatedEntities = ref({
  99. paths: [],
  100. forecasts: [],
  101. warnings: []
  102. });
  103. // 计算页面缩放比例
  104. const getScaleRatio = () => {
  105. const designWidth = 1920;
  106. const designHeight = 1080;
  107. return Math.min(window.innerWidth / designWidth, window.innerHeight / designHeight);
  108. };
  109. // 返回首页视角
  110. const goToHomeView = () => {
  111. if (viewer) {
  112. viewer.camera.flyTo({
  113. destination: Cesium.Cartesian3.fromDegrees(120.169103, 31.226174, 500000),
  114. orientation: {
  115. heading: Cesium.Math.toRadians(0),
  116. pitch: Cesium.Math.toRadians(-90),
  117. },
  118. duration: 1
  119. });
  120. }
  121. };
  122. // 切换POI点显示/隐藏
  123. const togglePOIDisplay = () => {
  124. poiVisible.value = !poiVisible.value;
  125. poiEntities.value.forEach(entity => {
  126. if (entity && entity.billboard) {
  127. entity.billboard.show = poiVisible.value;
  128. entity.label.show = poiVisible.value;
  129. }
  130. });
  131. };
  132. // 切换台风显示/隐藏并跳转视角
  133. const toggleTyphoon = () => {
  134. typhoonVisible.value = !typhoonVisible.value;
  135. // 控制台风相关实体显示/隐藏
  136. if (myEntityCollection.value) {
  137. myEntityCollection.value.show = typhoonVisible.value;
  138. }
  139. // 控制风圈显示/隐藏
  140. fengquanLayers.value.forEach(entity => {
  141. if (entity) {
  142. entity.show = typhoonVisible.value;
  143. }
  144. });
  145. // 控制路径线显示/隐藏
  146. typhoonRelatedEntities.value.paths.forEach(entity => {
  147. if (entity) {
  148. entity.show = typhoonVisible.value;
  149. }
  150. });
  151. // 控制预报路径显示/隐藏
  152. typhoonRelatedEntities.value.forecasts.forEach(entity => {
  153. if (entity) {
  154. entity.show = typhoonVisible.value;
  155. }
  156. });
  157. // 控制警戒线显示/隐藏
  158. typhoonRelatedEntities.value.warnings.forEach(entity => {
  159. if (entity) {
  160. entity.show = typhoonVisible.value;
  161. }
  162. });
  163. // 控制台风标记显示/隐藏
  164. if (tbentity.value) {
  165. tbentity.value.show = typhoonVisible.value;
  166. }
  167. // 如果台风隐藏,同时隐藏信息弹窗
  168. if (!typhoonVisible.value) {
  169. typhoonInfoVisible.value = false;
  170. }
  171. // 如果显示台风,则跳转到台风视角
  172. if (typhoonVisible.value) {
  173. viewer.camera.flyTo({
  174. destination: Cesium.Cartesian3.fromDegrees(120, 20, 4025692.0),
  175. });
  176. }
  177. };
  178. onMounted(async () => {
  179. // 设置HTML和body样式确保没有边距
  180. document.documentElement.style.height = '100%';
  181. document.body.style.height = '100%';
  182. document.body.style.margin = '0';
  183. document.body.style.padding = '0';
  184. // 初始化Cesium viewer
  185. viewer = new Cesium.Viewer('cesiumContainer', {
  186. timeline: false,
  187. baseLayer: false,
  188. geocoder: false,
  189. homeButton: false,
  190. sceneModePicker: false,
  191. navigationHelpButton: false,
  192. animation: false,
  193. fullscreenButton: false,
  194. vrButton: false,
  195. selectionIndicator: false,
  196. infoBox: false,
  197. // 加载地形效果
  198. terrainProvider: await Cesium.createWorldTerrainAsync({
  199. requestVertexNormals: true,
  200. })
  201. });
  202. // 多块倾斜摄影瓦片集的URL列表
  203. const tilesetUrls = [
  204. "http://localhost:9003/model/TSQ1234/tileset.json",
  205. "http://localhost:9003/model/SY123/tileset.json",
  206. ];
  207. // 存储加载成功的瓦片集
  208. const loadedTilesets = [];
  209. // 批量加载瓦片集
  210. async function loadMultipleTilesets() {
  211. try {
  212. const promises = tilesetUrls.map(async (url, index) => {
  213. try {
  214. const tileset = await Cesium.Cesium3DTileset.fromUrl(url, {
  215. maximumScreenSpaceError: 32,
  216. dynamicScreenSpaceError: true,
  217. skipLevelOfDetail: true,
  218. maximumConcurrentRequests: 5,
  219. tileCacheSize: 100
  220. });
  221. viewer.scene.primitives.add(tileset);
  222. loadedTilesets.push(tileset);
  223. console.log(`第${index + 1}块瓦片集加载完成`);
  224. return tileset;
  225. } catch (error) {
  226. console.error(`第${index + 1}块瓦片集加载失败:`, error);
  227. return null;
  228. }
  229. });
  230. await Promise.all(promises);
  231. console.log("所有瓦片集加载操作已完成");
  232. } catch (error) {
  233. console.error("批量加载逻辑出错:", error);
  234. }
  235. }
  236. // 执行加载
  237. loadMultipleTilesets();
  238. // 定义距离显示条件和缩放属性
  239. const distanceDisplayCondition = new Cesium.DistanceDisplayCondition(0, 10000000);
  240. const pointNearFarScalar = new Cesium.NearFarScalar(10000, 1.0, 1000000, 0.3);
  241. const labelNearFarScalar = new Cesium.NearFarScalar(10000, 1.0, 400000, 0);
  242. // 存储实体与数据的映射
  243. const entityDataMap = new Map();
  244. JYLData.forEach((item) => {
  245. const position = Cesium.Cartesian3.fromDegrees(
  246. parseFloat(item.LGTD),
  247. parseFloat(item.LTTD)
  248. );
  249. const entity = viewer.entities.add({
  250. position: position,
  251. billboard: {
  252. image: '/src/assets/icon/blue.png',
  253. scale: 0.4,
  254. color: Cesium.Color.YELLOW,
  255. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  256. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  257. distanceDisplayCondition: distanceDisplayCondition,
  258. scaleByDistance: pointNearFarScalar
  259. },
  260. label: {
  261. text: item.STNM || '未知点',
  262. font: '25px 微软雅黑',
  263. fillColor: Cesium.Color.WHITE,
  264. backgroundColor: new Cesium.Color(0.1, 0.1, 0.1, 0.7),
  265. backgroundPadding: new Cesium.Cartesian2(8, 4),
  266. showBackground: true,
  267. cornerRadius: 4,
  268. outlineColor: Cesium.Color.BLACK,
  269. outlineWidth: 1,
  270. horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
  271. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  272. pixelOffset: new Cesium.Cartesian2(0, -32),
  273. scale: 1.0,
  274. disableDepthTestDistance: Number.POSITIVE_INFINITY,
  275. distanceDisplayCondition: distanceDisplayCondition,
  276. scaleByDistance: labelNearFarScalar
  277. },
  278. id: `point-${item.STCD || item.LGTD + '-' + item.LTTD}`,
  279. properties: {
  280. data: item
  281. }
  282. });
  283. entityDataMap.set(entity.id, item);
  284. poiEntities.value.push(entity);
  285. });
  286. // 添加天地图图层
  287. const tdtLayer = new Cesium.WebMapTileServiceImageryProvider({
  288. url: `https://t0.tianditu.com/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}&tk=${TDTTK}`,
  289. layer: "tdt",
  290. style: "default",
  291. format: "image/jpeg",
  292. tileMatrixSetID: "w",
  293. maximumLevel: 16,
  294. show: true,
  295. });
  296. viewer.imageryLayers.addImageryProvider(tdtLayer);
  297. const tdtAnnotionLayer = new Cesium.WebMapTileServiceImageryProvider({
  298. url: `http://t0.tianditu.com/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}&tk=${TDTTK}`,
  299. layer: "tdtAnno",
  300. style: "default",
  301. format: "image/jpeg",
  302. tileMatrixSetID: "w",
  303. maximumLevel: 18,
  304. show: false,
  305. });
  306. viewer.imageryLayers.addImageryProvider(tdtAnnotionLayer);
  307. // 初始化视图
  308. viewer.cesiumWidget.creditContainer.style.display = "none";
  309. viewer.camera.setView({
  310. destination: Cesium.Cartesian3.fromDegrees(120.169103, 31.226174, 500000),
  311. orientation: {
  312. heading: Cesium.Math.toRadians(0),
  313. pitch: Cesium.Math.toRadians(-90),
  314. },
  315. });
  316. // 点击事件处理
  317. handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  318. handler.setInputAction((click) => {
  319. const scaleRatio = getScaleRatio();
  320. const correctedX = click.position.x / scaleRatio;
  321. const correctedY = click.position.y / scaleRatio;
  322. const pickedObject = viewer.scene.pick(new Cesium.Cartesian2(correctedX, correctedY));
  323. // 隐藏台风信息弹窗
  324. typhoonInfoVisible.value = false;
  325. if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id)) {
  326. const entityId = pickedObject.id.id;
  327. const data = entityDataMap.get(entityId) || pickedObject.id.properties?.data?.getValue();
  328. if (data) {
  329. selectedPoint.value = data;
  330. // 计算图标屏幕坐标
  331. const entityPosition = viewer.scene.cartesianToCanvasCoordinates(pickedObject.id.position._value);
  332. if (entityPosition) {
  333. popupPosition.value = {
  334. x: (entityPosition.x / scaleRatio) - 100,
  335. y: (entityPosition.y / scaleRatio) - 130
  336. };
  337. }
  338. viewer.flyTo(pickedObject.id, {
  339. offset: new Cesium.HeadingPitchRange(0, -0.5, 1500)
  340. });
  341. }
  342. } else {
  343. selectedPoint.value = null;
  344. }
  345. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  346. // 添加鼠标移动事件处理 - 用于台风路径点悬停
  347. handler.setInputAction((movement) => {
  348. // 只有台风可见时才处理台风信息弹窗
  349. if (!typhoonVisible.value) {
  350. typhoonInfoVisible.value = false;
  351. return;
  352. }
  353. const scaleRatio = getScaleRatio();
  354. const correctedX = movement.endPosition.x / scaleRatio;
  355. const correctedY = movement.endPosition.y / scaleRatio;
  356. const pickedObject = viewer.scene.pick(new Cesium.Cartesian2(correctedX, correctedY));
  357. // 检查是否悬停在台风路径点上
  358. if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id)) {
  359. const entityId = pickedObject.id.id;
  360. // 检查是否是台风路径点
  361. if (entityId && entityId.startsWith('typhoon-point-')) {
  362. const typhoonData = typhoonPointDataMap.value.get(entityId);
  363. if (typhoonData) {
  364. // 显示台风信息弹窗
  365. typhoonInfo.value = typhoonData;
  366. typhoonInfoVisible.value = true;
  367. // 计算弹窗位置
  368. const entityPosition = viewer.scene.cartesianToCanvasCoordinates(pickedObject.id.position._value);
  369. if (entityPosition) {
  370. typhoonPopupPosition.value = {
  371. x: (entityPosition.x / scaleRatio) - 120,
  372. y: (entityPosition.y / scaleRatio) - 200
  373. };
  374. }
  375. return; // 找到台风点后不再处理其他逻辑
  376. }
  377. }
  378. }
  379. // 如果不是台风路径点,隐藏台风信息弹窗
  380. typhoonInfoVisible.value = false;
  381. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  382. // 初始化台风相关功能
  383. initTyphoonVisualization();
  384. // 监听窗口大小变化,确保地图铺满
  385. window.addEventListener('resize', handleResize);
  386. // 初始触发一次 resize 确保地图正确显示
  387. handleResize();
  388. });
  389. // 初始化台风可视化
  390. const initTyphoonVisualization = () => {
  391. // 设置Cesium Ion访问令牌
  392. Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI1ODIwOGQ2Ny1hMTFhLTQ4OGQtODJhZi0wNmMzZGNhNjU5OWMiLCJpZCI6NTkzMTMsImlhdCI6MTYyMzk4ODQ4NX0.40CU0i0LswshdxVXAXEJgfEDJN3EK_jPbo_S8lece9E';
  393. // 创建台风数据源
  394. myEntityCollection.value = new Cesium.CustomDataSource("typhoonPoints");
  395. viewer.dataSources.add(myEntityCollection.value);
  396. // 初始化警戒线
  397. initJJ();
  398. // 加载台风数据
  399. initPoints();
  400. };
  401. // 加载台风数据
  402. const initPoints = () => {
  403. // 加载台风JSON数据
  404. const jsonUrl = new URL('../assets/Data/202508.json', import.meta.url).href;
  405. axios.get(jsonUrl)
  406. .then(response => {
  407. const typhoonData = response.data;
  408. // 处理台风数据,使用response.data.points
  409. processPoints(typhoonData.points, typhoonData);
  410. })
  411. .catch(error => {
  412. console.error('加载台风JSON数据失败,使用测试数据', error);
  413. // 测试数据 - 包含详细台风信息
  414. const testTyphoonData = {
  415. "name": "竹节草",
  416. "enname": "CO-MAY",
  417. "points": [
  418. {
  419. "time": "2025-07-23 14:00:00",
  420. "lng": "119.20",
  421. "lat": "18.40",
  422. "strong": "热带低压",
  423. "power": "7",
  424. "speed": "15",
  425. "pressure": "998",
  426. "movespeed": "14",
  427. "movedirection": "南西",
  428. "radius7": "150-280公里",
  429. "radius10": "--",
  430. "radius12": "--",
  431. "forecast": [
  432. {
  433. "tm": "中国",
  434. "forecastpoints": [
  435. { "lng": 121, "lat": 19 },
  436. { "lng": 122, "lat": 18 }
  437. ]
  438. }
  439. ]
  440. },
  441. {
  442. "time": "2025-07-23 17:00:00",
  443. "lng": "119.10",
  444. "lat": "18.00",
  445. "strong": "热带低压",
  446. "power": "7",
  447. "speed": "15",
  448. "pressure": "996",
  449. "movespeed": "13",
  450. "movedirection": "南南西",
  451. "radius7": "180-300公里",
  452. "radius10": "50-80公里",
  453. "radius12": "--",
  454. "forecast": []
  455. }
  456. ]
  457. };
  458. processPoints(testTyphoonData.points, testTyphoonData);
  459. });
  460. };
  461. // 处理台风数据
  462. const processPoints = (points, typhoonData) => {
  463. const lineArr = [];
  464. points.forEach((element, index) => {
  465. // 强制转换为数字类型
  466. const lng = Number(element.lng);
  467. const lat = Number(element.lat);
  468. let color = Cesium.Color.RED;
  469. // 根据台风强度设置颜色
  470. switch (element.strong) {
  471. case "热带低压":
  472. color = Cesium.Color.GREEN;
  473. break;
  474. case "热带风暴":
  475. color = Cesium.Color.BLUE;
  476. break;
  477. case "强热带风暴":
  478. color = Cesium.Color.YELLOW;
  479. break;
  480. case "台风":
  481. color = Cesium.Color.fromCssColorString("#FBC712");
  482. break;
  483. case "强台风":
  484. color = Cesium.Color.PLUM;
  485. break;
  486. case "超强台风":
  487. color = Cesium.Color.RED;
  488. break;
  489. }
  490. lineArr.push(lng, lat);
  491. // 为每个台风路径点创建唯一ID
  492. const pointId = `typhoon-point-${index}`;
  493. const entity = new Cesium.Entity({
  494. id: pointId,
  495. position: Cesium.Cartesian3.fromDegrees(lng, lat),
  496. point: {
  497. pixelSize: 8, // 增大点的尺寸,更容易被鼠标选中
  498. color: color,
  499. outlineColor: Cesium.Color.WHITE,
  500. outlineWidth: 2
  501. }
  502. });
  503. myEntityCollection.value.entities.add(entity);
  504. // 存储台风点与数据的映射关系,使用正确的属性名
  505. typhoonPointDataMap.value.set(pointId, {
  506. name: typhoonData.name || '',
  507. enname: typhoonData.enname || '',
  508. time: element.time || '',
  509. lng: element.lng,
  510. lat: element.lat,
  511. speed: element.speed,
  512. power: element.power,
  513. pressure: element.pressure,
  514. movedirection: element.movedirection,
  515. movespeed: element.movespeed,
  516. radius7: element.radius7 || '--',
  517. radius10: element.radius10 || '--',
  518. radius12: element.radius12 || '--'
  519. });
  520. });
  521. // 添加台风路径线
  522. const pathEntity = viewer.entities.add({
  523. polyline: {
  524. positions: Cesium.Cartesian3.fromDegreesArray(lineArr),
  525. width: 3,
  526. clampToGround: true,
  527. material: Cesium.Color.RED
  528. }
  529. });
  530. typhoonRelatedEntities.value.paths.push(pathEntity);
  531. // 添加台风登陆点标记
  532. if (typhoonData.land && typhoonData.land.length > 0) {
  533. typhoonData.land.forEach((landPoint, index) => {
  534. const landEntity = viewer.entities.add({
  535. position: Cesium.Cartesian3.fromDegrees(
  536. Number(landPoint.lng),
  537. Number(landPoint.lat)
  538. ),
  539. point: {
  540. pixelSize: 12,
  541. color: Cesium.Color.BLACK,
  542. outlineColor: Cesium.Color.YELLOW,
  543. outlineWidth: 2
  544. },
  545. label: {
  546. text: `登陆 ${index + 1}`,
  547. font: '14px 微软雅黑',
  548. fillColor: Cesium.Color.YELLOW,
  549. backgroundColor: Cesium.Color.BLACK.withAlpha(0.7),
  550. showBackground: true,
  551. pixelOffset: new Cesium.Cartesian2(0, -20)
  552. }
  553. });
  554. myEntityCollection.value.entities.add(landEntity);
  555. });
  556. }
  557. if (points.length > 0) {
  558. // 初始化预报路径
  559. initForeast(points[points.length - 1]);
  560. // 添加台风动画
  561. adds(points, typhoonData);
  562. }
  563. };
  564. // 初始化台风预报路径
  565. const initForeast = (data) => {
  566. const forecast = data.forecast || [];
  567. const colorArr = [
  568. Cesium.Color.fromCssColorString("#2D12FB"),
  569. Cesium.Color.fromCssColorString("#15E5E7"),
  570. Cesium.Color.fromCssColorString("#15E74A"),
  571. Cesium.Color.fromCssColorString("#E76F15"),
  572. Cesium.Color.fromCssColorString("#15D9E7")
  573. ];
  574. forecast.forEach((ele, ii) => {
  575. // 适配新的数据结构,预报点在tm对象下的forecastpoints中
  576. if (ele.forecastpoints && ele.forecastpoints.length > 0) {
  577. const lineArr = [];
  578. ele.forecastpoints.forEach(e => {
  579. // 强制转换为数字
  580. const lng = Number(e.lng);
  581. const lat = Number(e.lat);
  582. lineArr.push(lng, lat);
  583. const entity = new Cesium.Entity({
  584. position: Cesium.Cartesian3.fromDegrees(lng, lat),
  585. point: {
  586. pixelSize: 7,
  587. color: colorArr[ii % colorArr.length]
  588. }
  589. });
  590. myEntityCollection.value.entities.add(entity);
  591. });
  592. // 添加预报路径线
  593. const forecastEntity = viewer.entities.add({
  594. polyline: {
  595. positions: Cesium.Cartesian3.fromDegreesArray(lineArr),
  596. width: 2,
  597. clampToGround: true,
  598. material: new Cesium.PolylineDashMaterialProperty({
  599. color: colorArr[ii % colorArr.length]
  600. })
  601. }
  602. });
  603. typhoonRelatedEntities.value.forecasts.push(forecastEntity);
  604. }
  605. });
  606. };
  607. // 初始化警戒线
  608. const initJJ = () => {
  609. // 24小时警戒线
  610. const line24h = viewer.entities.add({
  611. name: '24小时警戒线',
  612. polyline: {
  613. positions: Cesium.Cartesian3.fromDegreesArray([
  614. 127, 34, 127, 22, 119, 18, 119, 11, 113, 4.5, 105, 0
  615. ]),
  616. width: 2,
  617. material: Cesium.Color.RED,
  618. clampToGround: true
  619. }
  620. });
  621. typhoonRelatedEntities.value.warnings.push(line24h);
  622. // 48小时警戒线
  623. const line48h = viewer.entities.add({
  624. name: '48小时警戒线',
  625. polyline: {
  626. positions: Cesium.Cartesian3.fromDegreesArray([
  627. 132, 34, 132, 22, 119, 0, 105, 0
  628. ]),
  629. width: 2,
  630. material: Cesium.Color.YELLOW,
  631. clampToGround: true
  632. }
  633. });
  634. typhoonRelatedEntities.value.warnings.push(line48h);
  635. // 警戒线标签
  636. const label24h = viewer.entities.add({
  637. position: Cesium.Cartesian3.fromDegrees(126.129019, 29.104287),
  638. label: {
  639. fillColor: Cesium.Color.RED,
  640. text: '24小时警戒线',
  641. font: '14pt monospace'
  642. }
  643. });
  644. typhoonRelatedEntities.value.warnings.push(label24h);
  645. const label48h = viewer.entities.add({
  646. position: Cesium.Cartesian3.fromDegrees(132, 20),
  647. label: {
  648. fillColor: Cesium.Color.YELLOW,
  649. text: '48小时警戒线',
  650. font: '14pt monospace'
  651. }
  652. });
  653. typhoonRelatedEntities.value.warnings.push(label48h);
  654. };
  655. // 添加台风动画
  656. const adds = (data, typhoonData) => {
  657. addTB(typhoonData);
  658. // 清除可能存在的旧定时器
  659. if (typhoonInterval) {
  660. clearInterval(typhoonInterval);
  661. }
  662. // 设置台风动画定时器
  663. typhoonInterval = setInterval(() => {
  664. // 只有台风可见时才更新动画
  665. if (!typhoonVisible.value) return;
  666. if (iii.value >= data.length) {
  667. iii.value = 0;
  668. }
  669. const kkk = iii.value * 2;
  670. // 确保经纬度是数字类型
  671. const currentData = data[iii.value];
  672. currentPointObj.value = {
  673. lon: Number(currentData.lng),
  674. lat: Number(currentData.lat),
  675. circle7: {
  676. radius1: 350 - kkk,
  677. radius2: 450 - kkk,
  678. radius3: 400 - kkk,
  679. radius4: 350 - kkk
  680. },
  681. circle10: {
  682. radius1: 250 - kkk,
  683. radius2: 270 - kkk,
  684. radius3: 250 - kkk,
  685. radius4: 220 - kkk
  686. },
  687. circle12: {
  688. radius1: 170 - kkk,
  689. radius2: 150 - kkk,
  690. radius3: 150 - kkk,
  691. radius4: 170 - kkk
  692. }
  693. };
  694. if (tbentity.value) {
  695. tbentity.value.position = Cesium.Cartesian3.fromDegrees(
  696. Number(currentData.lng),
  697. Number(currentData.lat)
  698. );
  699. }
  700. iii.value = (iii.value + 1) % data.length;
  701. removeTFLayer();
  702. // 只有当台风可见时才添加风圈
  703. if (typhoonVisible.value) {
  704. addTyphoonCircle();
  705. }
  706. }, 200);
  707. };
  708. // 添加台风标记
  709. const addTB = (typhoonData) => {
  710. // 尝试使用GIF动画标记
  711. const SuperGif = window.SuperGif;
  712. if (typeof SuperGif !== 'function') {
  713. console.warn('SuperGif未加载成功,使用默认标记');
  714. useDefaultMarker(typhoonData);
  715. return;
  716. }
  717. const img = document.createElement('img');
  718. img.src = '/tf.gif'; // public目录下的GIF
  719. img.onload = () => {
  720. try {
  721. const rub = new SuperGif({ gif: img });
  722. rub.load(() => {
  723. tbentity.value = viewer.entities.add({
  724. // 使用台风的中心经纬度作为初始位置
  725. position: Cesium.Cartesian3.fromDegrees(
  726. Number(typhoonData.centerlng || 123.75),
  727. Number(typhoonData.centerlat || 28.95)
  728. ),
  729. billboard: {
  730. image: new Cesium.CallbackProperty(() => {
  731. return rub.get_canvas().toDataURL('image/png');
  732. }, false),
  733. scale: 0.1
  734. }
  735. });
  736. });
  737. } catch (error) {
  738. console.error('GIF处理失败,使用默认标记', error);
  739. useDefaultMarker(typhoonData);
  740. }
  741. };
  742. img.onerror = () => {
  743. console.error('GIF加载失败,使用默认标记');
  744. useDefaultMarker(typhoonData);
  745. };
  746. };
  747. // 使用默认标记
  748. const useDefaultMarker = (typhoonData) => {
  749. tbentity.value = viewer.entities.add({
  750. position: Cesium.Cartesian3.fromDegrees(
  751. Number(typhoonData.centerlng || 123.75),
  752. Number(typhoonData.centerlat || 28.95)
  753. ),
  754. point: {
  755. pixelSize: 15,
  756. color: Cesium.Color.RED,
  757. outlineColor: Cesium.Color.WHITE,
  758. outlineWidth: 2
  759. }
  760. });
  761. };
  762. // 移除台风风圈图层
  763. const removeTFLayer = () => {
  764. fengquanLayers.value.forEach(entity => {
  765. viewer.entities.remove(entity);
  766. });
  767. fengquanLayers.value = [];
  768. };
  769. // 添加台风风圈
  770. const addTyphoonCircle = () => {
  771. if (!currentPointObj.value || !typhoonVisible.value) return;
  772. const circles = ['circle7', 'circle10', 'circle12'];
  773. circles.forEach(item => {
  774. const entity = viewer.entities.add({
  775. id: `tf_polygon_${item}`,
  776. polygon: {
  777. hierarchy: new Cesium.CallbackProperty(() => {
  778. const points = currentPointObj.value[item]
  779. ? getTyphoonPolygonPoints(currentPointObj.value, item)
  780. : [];
  781. return new Cesium.PolygonHierarchy(
  782. Cesium.Cartesian3.fromDegreesArray(points)
  783. );
  784. }, false),
  785. material: Cesium.Color.ORANGE.withAlpha(0.05),
  786. extrudedHeight: 1000,
  787. outline: true,
  788. outlineColor: Cesium.Color.ORANGE,
  789. outlineWidth: 2
  790. },
  791. polyline: {
  792. positions: new Cesium.CallbackProperty(() => {
  793. const points = currentPointObj.value[item]
  794. ? getTyphoonPolygonPoints(currentPointObj.value, item)
  795. : [];
  796. return Cesium.Cartesian3.fromDegreesArray(points);
  797. }, false),
  798. material: Cesium.Color.ORANGE,
  799. width: 2,
  800. height: 1000
  801. }
  802. });
  803. fengquanLayers.value.push(entity);
  804. });
  805. };
  806. // 生成台风风圈多边形点
  807. const getTyphoonPolygonPoints = (pointObj, cNum) => {
  808. const points = [];
  809. const center = [Number(pointObj.lon), Number(pointObj.lat)];
  810. const radiusList = [
  811. pointObj[cNum].radius1,
  812. pointObj[cNum].radius2,
  813. pointObj[cNum].radius3,
  814. pointObj[cNum].radius4
  815. ];
  816. const startAngleList = [0, 90, 180, 270];
  817. let fx, fy;
  818. startAngleList.forEach((startAngle, index) => {
  819. const radius = radiusList[index] / 100;
  820. const pointNum = 90;
  821. const endAngle = startAngle + 90;
  822. for (let i = 0; i <= pointNum; i++) {
  823. const angle = startAngle + ((endAngle - startAngle) * i) / pointNum;
  824. const sin = Math.sin((angle * Math.PI) / 180);
  825. const cos = Math.cos((angle * Math.PI) / 180);
  826. const x = center[0] + radius * sin;
  827. const y = center[1] + radius * cos;
  828. points.push(x, y);
  829. if (startAngle === 0 && i === 0) {
  830. fx = x;
  831. fy = y;
  832. }
  833. }
  834. });
  835. points.push(fx, fy);
  836. return points;
  837. };
  838. // 处理窗口大小变化
  839. const handleResize = () => {
  840. if (viewer) {
  841. viewer.resize();
  842. }
  843. };
  844. // 清理函数
  845. onUnmounted(() => {
  846. // 清除台风动画定时器
  847. if (typhoonInterval) {
  848. clearInterval(typhoonInterval);
  849. }
  850. // 移除窗口大小变化监听
  851. window.removeEventListener('resize', handleResize);
  852. // 销毁事件处理器
  853. if (handler) {
  854. handler.destroy();
  855. handler = null;
  856. }
  857. // 销毁viewer
  858. if (viewer && !viewer.isDestroyed()) {
  859. viewer.destroy();
  860. }
  861. });
  862. </script>
  863. <style scoped>
  864. /* 控制按钮样式 */
  865. .control-buttons {
  866. position: absolute;
  867. top: 200px;
  868. left: 150px;
  869. z-index: 100;
  870. display: flex;
  871. flex-direction: column;
  872. gap: 10px;
  873. }
  874. .control-btn {
  875. width: 40px;
  876. height: 40px;
  877. border-radius: 50%;
  878. background-color: rgba(255, 255, 255, 0.9);
  879. border: none;
  880. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  881. display: flex;
  882. align-items: center;
  883. justify-content: center;
  884. cursor: pointer;
  885. transition: all 0.2s ease;
  886. color: #333;
  887. }
  888. .control-btn:hover {
  889. background-color: white;
  890. transform: scale(1.1);
  891. }
  892. .control-btn .active {
  893. color: #1E88E5;
  894. }
  895. .custom-popup,
  896. .typhoon-popup {
  897. position: absolute;
  898. z-index: 1000;
  899. display: block;
  900. pointer-events: none;
  901. }
  902. .popup-content {
  903. background-color: white;
  904. border-radius: 5px;
  905. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  906. padding: 10px 15px;
  907. width: 240px;
  908. pointer-events: all;
  909. }
  910. .popup-arrow {
  911. position: absolute;
  912. bottom: -10px;
  913. left: 50%;
  914. transform: translateX(-50%);
  915. width: 0;
  916. height: 0;
  917. border-left: 10px solid transparent;
  918. border-right: 10px solid transparent;
  919. border-top: 10px solid white;
  920. }
  921. .popup-content h3 {
  922. margin-top: 0;
  923. margin-bottom: 8px;
  924. color: #333;
  925. font-size: 16px;
  926. }
  927. .popup-content p {
  928. margin: 5px 0;
  929. color: #666;
  930. font-size: 14px;
  931. }
  932. /* 台风图例样式 */
  933. .legend {
  934. position: absolute;
  935. z-index: 100;
  936. bottom: 30px;
  937. right: 10px;
  938. color: #fff;
  939. background: rgba(0, 0, 0, 0.8);
  940. list-style: none;
  941. padding: 10px 15px;
  942. border-radius: 4px;
  943. margin: 0;
  944. transition: opacity 0.3s ease, transform 0.3s ease;
  945. }
  946. .legend li {
  947. display: flex;
  948. align-items: center;
  949. margin: 5px 0;
  950. }
  951. .dot {
  952. border-radius: 50%;
  953. width: 10px;
  954. height: 10px;
  955. display: inline-block;
  956. margin-right: 8px;
  957. }
  958. .green {
  959. background: green;
  960. }
  961. .blue {
  962. background: blue;
  963. }
  964. .yellow {
  965. background: yellow;
  966. }
  967. .orange {
  968. background: #FBC712;
  969. }
  970. .red {
  971. background: red;
  972. }
  973. </style>