monitor_20201022154858.vue 7.2 KB

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