monitor_20201022165751.vue 8.9 KB

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