tool3DFlightSimulation.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. define(function () {
  2. /**
  3. * 状态值
  4. * @type {Boolean}
  5. * @default false
  6. * @private
  7. */
  8. var status = {
  9. initialized: false, //是否初始化
  10. };
  11. /**
  12. * 模块数据 用于数据存储和外部调用
  13. * @type {Object}
  14. * 数据存放
  15. */
  16. var modValue = {
  17. title: "模拟飞行",
  18. handler: null,
  19. pointArr: [],
  20. pointEntityArr: [],
  21. lineEntity: null,
  22. };
  23. /**
  24. *
  25. */
  26. function init() {
  27. if (!status.initialized) {
  28. subscribe();
  29. status.initialized = true;
  30. // map3DViewer.flightSimulation({
  31. // action: "add",
  32. // callback: callback
  33. // })
  34. }
  35. ONEMAP.C.publisher.publish({
  36. modName: 'tool3DFlightSimulation'
  37. }, 'tools:active');
  38. };
  39. /**
  40. * 注册监听事件
  41. */
  42. function subscribe() {
  43. ONEMAP.C.publisher.subscribe(remove, 'tools:active');
  44. ONEMAP.C.publisher.subscribe(removeAndClear, 'cleanMap');
  45. ONEMAP.C.publisher.subscribe(removeEvent, 'change23D');
  46. };
  47. // 23D转换
  48. function removeEvent() {
  49. map3DViewer.flightSimulation({
  50. action: "remove"
  51. })
  52. $('#toolFlightSimulationPanel').remove();
  53. if ($(".tools-flightSimulation").hasClass('cur')) {
  54. $(".tools-flightSimulation").removeClass('cur');
  55. }
  56. }
  57. /**
  58. * 3D通视 加载/移除 注册事件
  59. */
  60. function remove(options) {
  61. if (options.modName != 'tool3DFlightSimulation') {
  62. // if (options.modName == 'cleanMap') {
  63. // clear();
  64. // }
  65. removeAndClear();
  66. $(".tools-flightSimulation").removeClass('cur');
  67. } else {
  68. if ($(".tools-flightSimulation").hasClass('cur')) {
  69. $(".tools-flightSimulation").removeClass('cur');
  70. removeAndClear();
  71. } else {
  72. $(".tools-flightSimulation").addClass('cur');
  73. addPopup();
  74. }
  75. }
  76. }
  77. /**
  78. * 移除 模拟飞行
  79. */
  80. function removeAndClear() {
  81. map3DViewer.flightSimulation({
  82. action: "remove"
  83. })
  84. clearDrawEntity();
  85. if (modValue.handler) {
  86. modValue.handler.destroy()
  87. modValue.handler = null;
  88. }
  89. $('#toolFlightSimulationPanel').remove();
  90. };
  91. function drawLine() {
  92. var arr = modValue.pointArr.join(",").split(",")
  93. if (!modValue.lineEntity) {
  94. modValue.lineEntity = map3DViewer.map.entities.add({
  95. polyline: {
  96. positions: Cesium.Cartesian3.fromDegreesArray(arr),
  97. width: 10.0,
  98. material: new Cesium.PolylineGlowMaterialProperty({
  99. color: Cesium.Color.CHARTREUSE.withAlpha(.5)
  100. }),
  101. clampToGround: true
  102. }
  103. });
  104. } else {
  105. modValue.lineEntity.polyline.positions.setValue(Cesium.Cartesian3.fromDegreesArray(arr))
  106. }
  107. }
  108. function drawPoint(point) {
  109. var entity = map3DViewer.map.entities.add({
  110. position: Cesium.Cartesian3.fromDegrees(point.lng, point.lat),
  111. point: {
  112. pixelSize: 10,
  113. heightReference: 2,
  114. color: Cesium.Color.RED,
  115. }
  116. });
  117. modValue.pointEntityArr.push(entity);
  118. }
  119. function clearDrawEntity() {
  120. modValue.pointArr = []
  121. if (modValue.lineEntity) {
  122. map3DViewer.map.entities.remove(modValue.lineEntity)
  123. modValue.lineEntity = null
  124. }
  125. if (modValue.pointEntityArr.length > 0) {
  126. for (var i = 0; i < modValue.pointEntityArr.length; i++) {
  127. map3DViewer.map.entities.remove(modValue.pointEntityArr[i])
  128. }
  129. modValue.pointEntityArr = [];
  130. }
  131. }
  132. // 添加 弹窗
  133. function addPopup() {
  134. var html = '<div id="toolFlightSimulationPanel" class="viewHeightPopup class3d">' +
  135. ' <iframe frameborder="0" class="cover-iframe"></iframe>' +
  136. ' <div class="cover-content">' +
  137. ' <div class="">' +
  138. ' <div class="popup-lt"></div>' +
  139. ' <div class="popup-lb"></div>' +
  140. ' <div class="popup-rt"></div>' +
  141. ' <div class="popup-rb"></div>' +
  142. ' <div class="popup-ct">' +
  143. ' <button type="button" class="close"></button>' +
  144. ' <h3>' + modValue.title + '</h3>' +
  145. ' </div>' +
  146. ' <div class="popup-cb">' +
  147. ' <div class="panel">' +
  148. ' <div class="first step able">' +
  149. ' <div class="number">' +
  150. ' <span>1</span>' +
  151. ' </div>' +
  152. ' <div class="right">' +
  153. ' <span>请在地图上绘制路径</span>' +
  154. ' <button class="first_start first_button_change">绘制</button>' +
  155. ' <button class="first_again">重新绘制</button>' +
  156. ' <button class="first_end">结束</button>' +
  157. ' </div>' +
  158. ' </div>' +
  159. ' <div class="second step">' +
  160. ' <div class="number">' +
  161. ' <span>2</span>' +
  162. ' </div>' +
  163. ' <div class="right">' +
  164. ' <div>参数设置:</div>' +
  165. ' <div>' +
  166. ' <label for="">飞行高度</label>' +
  167. // ' <input class="fly_height" onkeyup="if(!/^\d+(\.\d+)?$/.test(this.value)){this.value=\'\'}else{this.value=this.value}" type="text">米' +
  168. ' <input class="fly_height" onkeyup="ONEMAP.M.tool3DFlightSimulation.judgeNumber(this,this.value)" type="text">米' +
  169. ' </div>' +
  170. ' <div>' +
  171. ' <label for="">飞行速度</label>' +
  172. ' <input class="fly_speed" onkeyup="ONEMAP.M.tool3DFlightSimulation.judgeNumber(this,this.value)" type="text">米' +
  173. ' </div>' +
  174. ' <div>' +
  175. ' 初始视角:' +
  176. ' <input type="radio" name="view" value="one" id="" checked>第一视角' +
  177. ' <input type="radio" name="view" value="two" id="">第二视角' +
  178. ' <input type="radio" name="view" value="god" id="">第三视角' +
  179. ' </div> ' +
  180. ' <div>' +
  181. ' <button class="simulate_start second_button_change">开始模拟</button>' +
  182. ' <button class="simulate_restart">重新模拟</button>' +
  183. ' </div>' +
  184. ' </div>' +
  185. ' </div>' +
  186. ' <div class="third step">' +
  187. ' <div class="number">' +
  188. ' <span>3</span>' +
  189. ' </div>' +
  190. ' <div class="right">' +
  191. ' <div>' +
  192. ' 飞行控制' +
  193. ' <button class="fly_pause">暂停</button>' +
  194. ' <button class="fly_start third_button_change">开始</button>' +
  195. ' </div>' +
  196. ' <div>' +
  197. ' 初始视角:' +
  198. ' <input type="radio" name="view_change" value="one" id="" checked>第一视角' +
  199. ' <input type="radio" name="view_change" value="two" id="">第二视角' +
  200. ' <input type="radio" name="view_change" value="god" id="">第三视角' +
  201. ' </div>' +
  202. ' </div>' +
  203. ' </div>' +
  204. ' </div>' +
  205. ' </div>' +
  206. ' </div>' +
  207. ' </div>' +
  208. '</div>';
  209. $("body").append(html);
  210. $("#toolFlightSimulationPanel .popup-ct").dragmove($('#toolFlightSimulationPanel'));
  211. // 开始绘制/重新绘制
  212. $('#toolFlightSimulationPanel .first_start,#toolFlightSimulationPanel .first_again').click(function () {
  213. // 重新绘制时删除之前添加模拟飞行
  214. if ($(this).hasClass("first_again")) {
  215. map3DViewer.flightSimulation({
  216. action: "remove"
  217. })
  218. }
  219. $(this).siblings(".first_end").addClass("first_button_change")
  220. $(this).removeClass("first_button_change")
  221. // 重置第二步
  222. resetSecondParam()
  223. // 重置第三步
  224. resetThirdParam()
  225. // 清除已绘制的图形
  226. clearDrawEntity()
  227. modValue.handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.scene.canvas);
  228. modValue.handler.setInputAction(function (click) {
  229. var pick1 = new Cesium.Cartesian2(click.position.x, click.position.y);
  230. var cartesian = map3DViewer.map.scene.globe.pick(map3DViewer.map.camera.getPickRay(pick1), map3DViewer.map.scene);
  231. if (cartesian) {
  232. var cartographic = map3DViewer.map.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  233. var lat = Cesium.Math.toDegrees(cartographic.latitude);
  234. var lng = Cesium.Math.toDegrees(cartographic.longitude);
  235. var alt = cartographic.height;
  236. var position = {
  237. lat: lat,
  238. lng: lng
  239. }
  240. modValue.pointArr.push([lng, lat])
  241. drawPoint(position);
  242. if (modValue.pointArr.length > 1) {
  243. drawLine();
  244. }
  245. }
  246. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  247. })
  248. // 结束绘制
  249. $('#toolFlightSimulationPanel .first_end').click(function () {
  250. if (!modValue.lineEntity) {
  251. clearDrawEntity()
  252. $(this).siblings(".first_start").addClass("first_button_change")
  253. $(this).removeClass("first_button_change")
  254. } else {
  255. $(this).siblings(".first_again").addClass("first_button_change")
  256. $(this).removeClass("first_button_change")
  257. if (modValue.handler) {
  258. modValue.handler.destroy()
  259. modValue.handler = null;
  260. }
  261. $('#toolFlightSimulationPanel .second').addClass("able")
  262. }
  263. })
  264. $('#toolFlightSimulationPanel .simulate_start,#toolFlightSimulationPanel .simulate_restart').click(function () {
  265. $(this).removeClass("second_button_change")
  266. if ($(this).hasClass("simulate_restart")) {
  267. $(this).addClass("second_button_change")
  268. map3DViewer.flightSimulation({
  269. action: "remove",
  270. })
  271. } else {
  272. $(this).siblings(".simulate_restart").addClass("second_button_change")
  273. }
  274. var height = Number($('#toolFlightSimulationPanel .fly_height').val())
  275. var speed = Number($('#toolFlightSimulationPanel .fly_speed').val())
  276. modValue.speed = speed
  277. var eye = $('#toolFlightSimulationPanel .second input[name="view"]:checked').val();
  278. // 使第三步的视角与初始化视角相同
  279. $('#toolFlightSimulationPanel .third input[value="' + eye + '"]').prop("checked", 'checked');
  280. //重置第三步
  281. resetThirdParam()
  282. $('#toolFlightSimulationPanel .third').addClass("able")
  283. $('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
  284. $('#toolFlightSimulationPanel .third .fly_pause').addClass("third_button_change")
  285. map3DViewer.flightSimulation({
  286. action: "add",
  287. eyeslook: eye,
  288. height: height,
  289. speed: speed,
  290. lineCoor: modValue.pointArr,
  291. lineColor: "#00ffcc",
  292. outlineColor: "#FFFFFF",
  293. modelUrl: "./data/airplane.glb",
  294. callback: function () {}
  295. })
  296. })
  297. // 模拟继续
  298. $('#toolFlightSimulationPanel .fly_start').click(function () {
  299. $('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
  300. $('#toolFlightSimulationPanel .third .fly_pause').addClass("third_button_change")
  301. map3DViewer.flightSimulation({
  302. action: "resetCondition",
  303. speed: modValue.speed
  304. })
  305. })
  306. // 模拟暂停
  307. $('#toolFlightSimulationPanel .fly_pause').click(function () {
  308. $('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
  309. $('#toolFlightSimulationPanel .third .fly_start').addClass("third_button_change")
  310. map3DViewer.flightSimulation({
  311. action: "resetCondition",
  312. speed: 0
  313. })
  314. })
  315. // 切换视角
  316. $('#toolFlightSimulationPanel .third input[name="view_change"]').click(function () {
  317. var eye = $(this).val();
  318. // one two god
  319. map3DViewer.flightSimulation({
  320. action: "changeView",
  321. eyeslook: eye
  322. })
  323. })
  324. // 关闭
  325. $('#toolFlightSimulationPanel .close').click(function () {
  326. removeAndClear()
  327. if ($(".tools-flightSimulation").hasClass('cur')) {
  328. $(".tools-flightSimulation").removeClass('cur');
  329. }
  330. })
  331. }
  332. // 重置第二步
  333. function resetSecondParam() {
  334. $('#toolFlightSimulationPanel .second').removeClass("able")
  335. $('#toolFlightSimulationPanel .second .second_button_change').removeClass("second_button_change")
  336. $('#toolFlightSimulationPanel .second .simulate_start').addClass("second_button_change")
  337. }
  338. // 重置第三步
  339. function resetThirdParam() {
  340. $('#toolFlightSimulationPanel .third').removeClass("able")
  341. $('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
  342. $('#toolFlightSimulationPanel .third .fly_start').addClass("third_button_change")
  343. }
  344. // 判断是否为非负实数
  345. function judgeNumber(docu, value) {
  346. if (isNaN(Number(value))) {
  347. docu.value = ""
  348. } else {
  349. if (/^\d+(\.\d+)?$/.test(value)) {
  350. docu.value = value
  351. }
  352. }
  353. }
  354. return ONEMAP.M.tool3DFlightSimulation = {
  355. init: init,
  356. judgeNumber: judgeNumber
  357. }
  358. })