monitor_20201022144321.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. weather:null,
  26. publicPath: process.env.BASE_URL,
  27. initPosition: [106.0231304, 37.73323706, 0],
  28. viewer: null,
  29. };
  30. },
  31. mounted() {
  32. var viewer = new Cesium.Viewer("cesiumContainer", {
  33. animation: false, //是否显示动画控件
  34. shouldAnimate: true,
  35. homeButton: false, //是否显示Home按钮
  36. fullscreenButton: false, //是否显示全屏按钮
  37. baseLayerPicker: false, //是否显示图层选择控件
  38. geocoder: false, //是否显示地名查找控件http://localhost:8080/#/cesiumScene
  39. timeline: false, //是否显示时间线控件
  40. sceneModePicker: false, //是否显示投影方式控件
  41. navigationHelpButton: false, //是否显示帮助信息控件
  42. infoBox: false, //是否显示点击要素之后显示的信息
  43. requestRenderMode: true, //启用请求渲染模式
  44. scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
  45. sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式 Cesium.SceneMode
  46. fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处
  47. imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
  48. url:
  49. "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
  50. baseLayerPicker: false,
  51. }),
  52. });
  53. viewer.scene.camera.setView({
  54. // 初始化相机经纬度
  55. destination: new Cesium.Cartesian3.fromDegrees(
  56. 106.0231304,
  57. 37.73323706,
  58. 2000
  59. ),
  60. orientation: {
  61. heading: Cesium.Math.toRadians(0.0),
  62. pitch: Cesium.Math.toRadians(-90.0), //从上往下看为-90
  63. roll: 0,
  64. },
  65. });
  66. this.viewer = viewer;
  67. },
  68. methods: {
  69. addRain() {
  70. // 雨雪天气添加
  71. // scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createRainStage())
  72. var scene = this.viewer.scene;
  73. let weather = this.weather;
  74. if (weather == null) {
  75. weather =scene.postProcessStages.add(
  76. Cesium.PostProcessStageLibrary.createSnowStage()
  77. );
  78. console.log(weather)
  79. }else{
  80. console.log(11111)
  81. scene.postProcessStages.remove(weather);
  82. weather = null;
  83. }
  84. // scene.skyAtmosphere.hueShift = -0.8;
  85. // scene.skyAtmosphere.saturationShift = -0.7;
  86. // scene.skyAtmosphere.brightnessShift = -0.33;
  87. // scene.fog.density = 0.001;
  88. // scene.fog.minimumBrightness = 0.8;
  89. },
  90. geoJSON() {
  91. //jeojson 加载宁夏地图
  92. Cesium.Math.setRandomNumberSeed(0);
  93. var promise = this.viewer.dataSources.add(
  94. Cesium.GeoJsonDataSource.load(
  95. `${this.publicPath}static/mapgeoJsonnx.json`,
  96. {
  97. stroke: Cesium.Color.RAD,
  98. fill: Cesium.Color.TRANSPARENT,
  99. strokeWidth: 3,
  100. markerSymbol: "?",
  101. }
  102. )
  103. );
  104. },
  105. flyto() {
  106. this.viewer.camera.flyTo({
  107. destination: Cesium.Cartesian3.fromDegrees(
  108. 106.0231304,
  109. 37.73323706,
  110. 2000
  111. ),
  112. //duration:5, // 设置飞行持续时间,默认会根据距离来计算
  113. complete: function () {
  114. // 到达位置后执行的回调函数
  115. console.log("到达目的地");
  116. },
  117. cancle: function () {
  118. // 如果取消飞行则会调用此函数
  119. console.log("飞行取消");
  120. },
  121. pitchAdjustHeight: -90, // 如果摄像机飞越高于该值,则调整俯仰俯仰的俯仰角度,并将地球保持在视口中。
  122. maximumHeight: 5000, // 相机最大飞行高度
  123. });
  124. },
  125. handleCommand(command) {
  126. switch (command) {
  127. case "a":
  128. this.addRain();
  129. break;
  130. case "b":
  131. this.geoJSON();
  132. break;
  133. default:
  134. this.flyto();
  135. break;
  136. }
  137. },
  138. //初始化weosocket
  139. initWebSocket() {
  140. if (typeof WebSocket === "undefined") {
  141. alert("您的浏览器不支持WebSocket");
  142. return false;
  143. }
  144. const wsuri = "ws://10.155.32.4:8010/shbracelet";
  145. this.websock = new WebSocket(wsuri);
  146. this.websock.onopen = this.websocketonopen;
  147. this.websock.onmessage = this.websocketonmessage;
  148. this.websock.onerror = this.websocketonerror;
  149. this.websock.onclose = this.websocketclose;
  150. },
  151. //连接成功
  152. websocketonopen() {
  153. console.log("WebSocket连接成功");
  154. // 添加心跳检测,每30秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
  155. let self = this;
  156. this.timer = setInterval(() => {
  157. try {
  158. self.websock.send("test");
  159. console.log("发送消息");
  160. } catch (err) {
  161. console.log("断开了:" + err);
  162. self.connection();
  163. }
  164. }, 30000);
  165. },
  166. //接收后端返回的数据,可以根据需要进行处理
  167. websocketonmessage(e) {
  168. var vm = this;
  169. let res = JSON.parse(e.data);
  170. console.log(res);
  171. },
  172. //连接建立失败重连
  173. websocketonerror(e) {
  174. console.log(`连接失败的信息:`, e);
  175. this.initWebSocket(); // 连接失败后尝试重新连接
  176. },
  177. //关闭连接
  178. websocketclose(e) {
  179. console.log("断开连接", e);
  180. },
  181. },
  182. };
  183. </script>
  184. <!-- Add "scoped" attribute to limit CSS to this component only -->
  185. <style lang="scss" scoped>
  186. html,
  187. body,
  188. #cesiumContainer {
  189. width: 100%;
  190. height: 100%;
  191. margin: 0;
  192. padding: 0;
  193. overflow: hidden;
  194. }
  195. .box {
  196. height: 100%;
  197. .menu {
  198. position: absolute;
  199. left: 20px;
  200. top: 20px;
  201. background-color: rgba(0, 0, 0, 0.5);
  202. }
  203. }
  204. </style>