map.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //DGE 是自主研发数字孪生框架
  2. //DGE 作者:tianhongguo
  3. let map = new DGE.Map('cesiumContainer', {
  4. animation:false, //左下角的动画仪表盘
  5. baseLayerPicker:false, //右上角的图层选择按钮
  6. geocoder:false, //搜索框
  7. homeButton:false, //home按钮
  8. sceneModePicker:false, //模式切换按钮
  9. timeline:false, //底部的时间轴
  10. navigationHelpButton:false, //右上角的帮助按钮,
  11. fullscreenButton:false, //右下角的全屏按钮
  12. infoBox: false, //是否显示点击要素之后显示的信息
  13. vrButton: true,
  14. infoBox: false,
  15. selectionIndicator: false,
  16. baseUrl:"./Source/"
  17. });
  18. map.viewer._cesiumWidget._creditContainer.style.display = "none";
  19. let tileset=map.data.threeDTiles.load({
  20. url:"https://testgis.4dage.com/liangqichao3dtiles1/tileset.json",
  21. height:10,
  22. maximumScreenSpaceError:40
  23. });
  24. map.viewer.camera.setView({
  25. destination:new Global.Cartesian3.fromDegrees(line[0].lng,line[0].lat,line[0].alt),
  26. orientation:{
  27. heading : line[0].heading,
  28. pitch : line[0].pitch,
  29. roll : line[0].roll
  30. }
  31. });
  32. let options={};
  33. // 用于在使用重置导航重置地图视图时设置默认视图控制。接受的值是Cesium.Cartographic 和 Cesium.Rectangle.
  34. options.defaultResetView =Global.Rectangle.fromDegrees(80, 22, 130, 50);
  35. // 用于启用或禁用罗盘。true是启用罗盘,false是禁用罗盘。默认值为true。如果将选项设置为false,则罗盘将不会添加到地图中。
  36. options.enableCompass= true;
  37. // 用于启用或禁用缩放控件。true是启用,false是禁用。默认值为true。如果将选项设置为false,则缩放控件将不会添加到地图中。
  38. options.enableZoomControls= false;
  39. // 用于启用或禁用距离图例。true是启用,false是禁用。默认值为true。如果将选项设置为false,距离图例将不会添加到地图中。
  40. options.enableDistanceLegend= false;
  41. // 用于启用或禁用指南针外环。true是启用,false是禁用。默认值为true。如果将选项设置为false,则该环将可见但无效。
  42. options.enableCompassOuterRing= true;
  43. DGE.CesiumNavigation(map.viewer, options);
  44. let route =new DGE.FlyRoute("测试路线","FlyRouteId");
  45. line.map(({
  46. lng,
  47. lat,
  48. alt,
  49. heading,
  50. pitch,
  51. roll,
  52. waitTime,
  53. }) => {
  54. let step = new DGE.FlyStep(
  55. new DGE.P3(lng, lat, alt),
  56. heading,
  57. pitch,
  58. roll,
  59. waitTime
  60. );
  61. route.add(step); // 给飞行路线添加站点
  62. });
  63. route.speed = 2;
  64. map.flyManager.activeRoute = route;
  65. map.flyManager.add(route);
  66. var flyShow=false;
  67. var interval=null;
  68. var index=0;
  69. addLabels();
  70. //添加地名
  71. function addLabels(){
  72. ps.map(p=>{
  73. map.viewer.entities.add({
  74. id:p.name,
  75. position:Global.Cartesian3.fromDegrees(p.lng,p.lat,p.alt),
  76. label:{
  77. text:p.name,
  78. pixelOffset:new Global.Cartesian2(0,-22),
  79. scale:0.6,
  80. fillColor: Global.Color.WHITE
  81. },
  82. billboard:{
  83. image:"./imgs/label.png",
  84. scale:0.8
  85. },
  86. polyline:{
  87. positions:[Global.Cartesian3.fromDegrees(p.lng,p.lat,p.alt),Global.Cartesian3.fromDegrees(p.lng,p.lat,10)],
  88. material:new Global.Color(238/255,193/255,133/255)
  89. }
  90. });
  91. })
  92. intervalFun();
  93. }
  94. function intervalFun(){
  95. var j=0;
  96. interval=setInterval(()=>{
  97. if(flyShow){
  98. let ens=map.viewer.entities.values;
  99. for(let i=0;i<line.length;i++){
  100. if(i==map.flyManager.activeStepIndex){
  101. ens.map(e=>{
  102. e.label.fillColor=Global.Color.WHITE;
  103. e.billboard.image="./imgs/label.png";
  104. if(e.id==line[i].name) {
  105. e.billboard.image="./imgs/labelok.png";
  106. e.label.fillColor= new Global.Color(157/255,96/255,34/255);
  107. }
  108. })
  109. }
  110. }
  111. if(index==0&&j==0){
  112. addStr(line[index].str)
  113. }
  114. if(index!=map.flyManager.activeStepIndex){
  115. j=0;
  116. index=map.flyManager.activeStepIndex;
  117. if(line[index].str){
  118. addStr(line[index].str)
  119. }
  120. }
  121. j++;
  122. }
  123. },1000);
  124. }
  125. function addStr(str){
  126. var txtdiv=document.getElementById("font");
  127. while(txtdiv.hasChildNodes())
  128. {
  129. txtdiv.removeChild(txtdiv.firstChild);
  130. }
  131. if(str!="false"){
  132. var txt=document.createTextNode(str);
  133. txtdiv.appendChild(txt);
  134. }
  135. var width=(window.innerWidth-txtdiv.clientWidth)/2;
  136. txtdiv.style.left=width;
  137. let styleStr="position: absolute;z-index: 100;bottom: 10%;background-color:rgba(157,96,34,0.5);color:white;left:"+width+"px;"
  138. txtdiv.setAttribute('style',styleStr);
  139. }
  140. //清除
  141. function clear(){
  142. clearInterval(interval);
  143. var txtdiv=document.getElementById("font");
  144. while(txtdiv.hasChildNodes())
  145. {
  146. txtdiv.removeChild(txtdiv.firstChild);
  147. }
  148. txtdiv.display="none"
  149. }
  150. //开始播放
  151. function play(){
  152. map.flyManager.play();
  153. flyShow=true;
  154. var txtdiv=document.getElementById("font");
  155. txtdiv.display="block";
  156. intervalFun();
  157. }
  158. //暂停播放
  159. function pause(){
  160. map.flyManager.pause();
  161. }
  162. //继续播放
  163. function continueDo(){
  164. map.flyManager.continue();
  165. }
  166. //停止播放
  167. function stop(){
  168. map.flyManager.stop();
  169. clear();
  170. }
  171. // var handler = new Global.ScreenSpaceEventHandler(map.viewer.scene.canvas);
  172. // handler.setInputAction(function (event) {
  173. // //1.椭球面坐标:获取当前点击视线与椭球面相交处的坐标,在加载地形的场景上获取的坐标有误差
  174. // //var earthPosition = viewer.camera.pickEllipsoid(event.position,viewer.scene.globe.ellipsoid);
  175. // //2.场景坐标:获取场景中任意点击处的对应的世界坐标,需要开启“地形深度检测”(在未开启“地形深度检测”的情况下只能在3DTile上准确获取空间坐标,开启“地形深度检测”后,viewer.scene.pickPosition 也能在非3DTile上准确获取坐标)
  176. // map.viewer.scene.globe.depthTestAgainstTerrain = true;
  177. // var ellipsoid = map.viewer.scene.globe.ellipsoid;
  178. // var cartographic = ellipsoid.cartesianToCartographic(map.viewer.camera.position);
  179. // var lat = Global.Math.toDegrees(cartographic.latitude);
  180. // var lng = Global.Math.toDegrees(cartographic.longitude);
  181. // var alt = cartographic.height;
  182. // console.log({
  183. // lat:lat,
  184. // lng:lng,
  185. // alt:alt,
  186. // heading:map.viewer.camera.heading,
  187. // pitch:map.viewer.camera.pitch,
  188. // roll:map.viewer.camera.roll
  189. // })
  190. // }, Global.ScreenSpaceEventType.LEFT_CLICK);
  191. document.getElementById("fun").addEventListener("click",()=>{
  192. play()
  193. })
  194. document.getElementById("fun1").addEventListener("click",()=>{
  195. pause()
  196. })
  197. document.getElementById("fun2").addEventListener("click",()=>{
  198. continueDo()
  199. })
  200. document.getElementById("fun3").addEventListener("click",()=>{
  201. stop();
  202. })