monitor_20201022164746.vue 8.8 KB

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