monitor_20201022115808.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div id="container" class="box">
  3. <div id="cesiumContainer"></div>
  4. <div class="menu">
  5. <el-dropdown @command="handleCommand">
  6. <span class="el-dropdown-link">
  7. 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
  8. </span>
  9. <el-dropdown-menu slot="dropdown">
  10. <el-dropdown-item command="a">天气开关</el-dropdown-item>
  11. <el-dropdown-item command="b">地图中文标记</el-dropdown-item>
  12. <el-dropdown-item command="c">回到初始位置</el-dropdown-item>
  13. </el-dropdown-menu>
  14. </el-dropdown>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. var Cesium = require("cesium/Cesium");
  20. var widgets = require("cesium/Widgets/widgets.css");
  21. export default {
  22. name: "cesiumPage",
  23. data() {
  24. return {
  25. publicPath: process.env.BASE_URL,
  26. initPosition: [106.0231304, 37.73323706, 0],
  27. viewer: null,
  28. };
  29. },
  30. mounted() {
  31. var viewer = new Cesium.Viewer("cesiumContainer", {
  32. animation: false, //是否显示动画控件
  33. shouldAnimate: true,
  34. homeButton: false, //是否显示Home按钮
  35. fullscreenButton: false, //是否显示全屏按钮
  36. baseLayerPicker: false, //是否显示图层选择控件
  37. geocoder: false, //是否显示地名查找控件http://localhost:8080/#/cesiumScene
  38. timeline: false, //是否显示时间线控件
  39. sceneModePicker: false, //是否显示投影方式控件
  40. navigationHelpButton: false, //是否显示帮助信息控件
  41. infoBox: false, //是否显示点击要素之后显示的信息
  42. requestRenderMode: true, //启用请求渲染模式
  43. scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
  44. sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式 Cesium.SceneMode
  45. fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处
  46. imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
  47. url:
  48. "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
  49. baseLayerPicker: false,
  50. }),
  51. });
  52. viewer.scene.camera.setView({
  53. // 初始化相机经纬度
  54. destination: new Cesium.Cartesian3.fromDegrees(
  55. 106.0231304,
  56. 37.73323706,
  57. 2000
  58. ),
  59. orientation: {
  60. heading: Cesium.Math.toRadians(0.0),
  61. pitch: Cesium.Math.toRadians(-90.0), //从上往下看为-90
  62. roll: 0,
  63. },
  64. });
  65. this.viewer = viewer;
  66. },
  67. methods: {
  68. addRain() {
  69. // 雨雪天气添加
  70. var scene = this.viewer.scene;
  71. // scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createSnowStage())
  72. // scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createRainStage())
  73. scene.skyAtmosphere.hueShift = -0.8;
  74. scene.skyAtmosphere.saturationShift = -0.7;
  75. scene.skyAtmosphere.brightnessShift = -0.33;
  76. scene.fog.density = 0.001;
  77. scene.fog.minimumBrightness = 0.8;
  78. },
  79. geoJSON() {
  80. //jeojson 加载宁夏地图
  81. Cesium.Math.setRandomNumberSeed(0);
  82. var promise = this.viewer.dataSources.add(
  83. Cesium.GeoJsonDataSource.load(
  84. `${this.publicPath}static/mapgeoJsonnx.json`,
  85. {
  86. stroke: Cesium.Color.RAD,
  87. fill: Cesium.Color.TRANSPARENT,
  88. strokeWidth: 3,
  89. markerSymbol: "?",
  90. }
  91. )
  92. );
  93. },
  94. flyto() {
  95. this.viewer.camera.flyTo({
  96. destination: Cesium.Cartesian3.fromDegrees(-117.16, 32.71, 2000.0),
  97. });
  98. },
  99. handleCommand(command) {
  100. switch (command) {
  101. case a:
  102. this.addRain();
  103. break;
  104. case b:
  105. this.geoJSON();
  106. break;
  107. default:
  108. this.flyto();
  109. break;
  110. }
  111. },
  112. //初始化weosocket
  113. initWebSocket() {
  114. if (typeof WebSocket === "undefined") {
  115. alert("您的浏览器不支持WebSocket");
  116. return false;
  117. }
  118. const wsuri = "ws://10.155.32.4:8010/shbracelet";
  119. this.websock = new WebSocket(wsuri);
  120. this.websock.onopen = this.websocketonopen;
  121. this.websock.onmessage = this.websocketonmessage;
  122. this.websock.onerror = this.websocketonerror;
  123. this.websock.onclose = this.websocketclose;
  124. },
  125. //连接成功
  126. websocketonopen() {
  127. console.log("WebSocket连接成功");
  128. // 添加心跳检测,每30秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
  129. let self = this;
  130. this.timer = setInterval(() => {
  131. try {
  132. self.websock.send("test");
  133. console.log("发送消息");
  134. } catch (err) {
  135. console.log("断开了:" + err);
  136. self.connection();
  137. }
  138. }, 30000);
  139. },
  140. //接收后端返回的数据,可以根据需要进行处理
  141. websocketonmessage(e) {
  142. var vm = this;
  143. let res = JSON.parse(e.data);
  144. console.log(res);
  145. },
  146. //连接建立失败重连
  147. websocketonerror(e) {
  148. console.log(`连接失败的信息:`, e);
  149. this.initWebSocket(); // 连接失败后尝试重新连接
  150. },
  151. //关闭连接
  152. websocketclose(e) {
  153. console.log("断开连接", e);
  154. },
  155. },
  156. };
  157. </script>
  158. <!-- Add "scoped" attribute to limit CSS to this component only -->
  159. <style lang="scss" scoped>
  160. html,
  161. body,
  162. #cesiumContainer {
  163. width: 100%;
  164. height: 100%;
  165. margin: 0;
  166. padding: 0;
  167. overflow: hidden;
  168. }
  169. .box {
  170. height: 100%;
  171. .menu {
  172. position: absolute;
  173. left: 20px;
  174. top: 20px;
  175. background-color: rgba(0, 0, 0, 0.5);
  176. }
  177. }
  178. </style>