CesiumMap.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <el-dialog v-if="isShow"
  3. :model-value="isShow"
  4. title="地理信息"
  5. :width="1200"
  6. :close-on-click-modal="false"
  7. :before-close="handleClose"
  8. >
  9. <div id="mapBox" >
  10. <!--<div id="mapOperation">-->
  11. <!-- <el-button>点标记</el-button>-->
  12. <!-- <el-button>线标记</el-button>-->
  13. <!-- <el-button>面标记</el-button>-->
  14. <!--</div>-->
  15. <div id="mapContainer">
  16. </div>
  17. </div>
  18. <template #footer>
  19. <el-button type="primary">确认</el-button>
  20. </template>
  21. </el-dialog>
  22. </template>
  23. <script>
  24. import 'cesium/Widgets/widgets.css'
  25. import CesiumMarker from "@/static/js/CesiumMarker";
  26. export default {
  27. data() {
  28. return {
  29. viewer: '',
  30. drawData: {
  31. points: [],
  32. lines: [],
  33. polygons: []
  34. },
  35. cesiumConfig: {
  36. animation: false, //动画控制不显示
  37. timeline: false, //时间线不显示
  38. fullscreenButton: false, //全屏按钮不显示
  39. imageryProvider: new Cesium.SingleTileImageryProvider({
  40. url: (function createColorCanvas(color) {
  41. let width = 1,
  42. height = 1;
  43. let canvas = document.createElement("canvas");
  44. canvas.width = width;
  45. canvas.height = height;
  46. let ctx = canvas.getContext("2d");
  47. ctx.fillStyle = color;
  48. ctx.fillRect(0, 0, width, height);
  49. return canvas.toDataURL();
  50. })("#ffffff00"),
  51. rectangle: Cesium.Rectangle.fromDegrees(-180.0, -90.0, 180.0, 90.0),
  52. }),
  53. infoBox: false,
  54. baseLayerPicker: false, //地图切换不显示
  55. geocoder: false,
  56. homeButton: false,
  57. selectionIndicator: false, // 去除绿色选择框
  58. sceneModePicker: false,
  59. navigationHelpButton: false,
  60. navigationInstructionsInitiallyVisible:false,
  61. scene3DOnly: true, // 仅以3D渲染以节省GPU内存
  62. useBrowserRecommendedResolution: true, // 以浏览器建议的分辨率渲染
  63. },
  64. }
  65. },
  66. props: {
  67. isShow: Boolean,
  68. item: Object,
  69. close: Function
  70. },
  71. watch: {
  72. "isShow": function (val) {
  73. this.initMap();
  74. }
  75. },
  76. mounted() {
  77. this.$util.loading.handleLoading(true)
  78. this.initMap();
  79. },
  80. methods: {
  81. initMap() {
  82. let app = this;
  83. this.$nextTick(()=>{
  84. app.viewer = new Cesium.Viewer('mapContainer', app.cesiumConfig);
  85. app.viewer.scene.preRender.addEventListener(function () {
  86. app.loading = true;
  87. })
  88. app.viewer.scene.postRender.addEventListener(function () {
  89. app.loading = false;
  90. })
  91. // 加载模型
  92. if (app.item.url && app.item.url!=='') {
  93. app.add3DTiles(app.viewer, app.item.url)
  94. } else {
  95. app.$util.loading.handleLoading(false)
  96. }
  97. });
  98. },
  99. // 添加模型
  100. add3DTiles(viewer, url){
  101. let app = this;
  102. let tileset = new Cesium.Cesium3DTileset({
  103. url: url,
  104. skipLevelOfDetail: true, //开启跳级加载
  105. maximumScreenSpaceError: 16,
  106. maximumNumberOfLoadedTiles: 2000,
  107. maximumMemoryUsage: 512,//tileset可以使用的最大内存
  108. show: true,
  109. immediatelyLoadDesiredLevelOfDetail: true,
  110. });
  111. tileset.allTilesLoaded.addEventListener(function() {
  112. app.$util.loading.handleLoading(false)
  113. });
  114. tileset.tileFailed.addEventListener(function (err) {
  115. app.$util.loading.handleLoading(false);
  116. app.$message({message: '模型加载失败', type: 'error'});
  117. app.handleClose()
  118. })
  119. viewer.scene.primitives.add(tileset);
  120. tileset.show=true;
  121. tileset.readyPromise.then(function () {
  122. viewer.zoomTo(tileset, {
  123. heading: 2.718565,
  124. pitch: -0.415366,
  125. roll: 0.0,
  126. });
  127. }).catch(err=>{
  128. app.$util.loading.handleLoading(false);
  129. app.$message({dangerouslyUseHTMLString: true, message: '模型加载失败,'+err.statusCode+':'+err.response, type: 'error'});
  130. app.handleClose()
  131. });
  132. return tileset;
  133. },
  134. handleClose() {
  135. this.close(false)
  136. }
  137. }
  138. }
  139. </script>
  140. <style scoped>
  141. #mapBox {
  142. width: 100%;
  143. height: 100%;
  144. }
  145. #mapContainer {
  146. width: 100%;
  147. height: 100%;
  148. }
  149. #mapOperation {
  150. margin-bottom: 10px;
  151. }
  152. </style>
  153. <style>
  154. /*隐藏 cesium logo*/
  155. .cesium-widget-credits{
  156. display: none !important;
  157. }
  158. .el-dialog__body {
  159. padding-top: 10px;
  160. }
  161. </style>