index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div class="container">
  3. <el-row style="height:50px;">
  4. <div class="header" >
  5. <el-button :type="(this.dimension==2?'success':'danger')" @click="dimensionswitch(2)" round>二维</el-button>
  6. <el-button :type="(this.dimension==3?'success':'danger')" @click="dimensionswitch(3)" round>三维</el-button>
  7. <el-button :type="(this.dimension==5?'success':'danger')" @click="dimensionswitch(5)" round>一体化</el-button>
  8. </div>
  9. </el-row>
  10. <el-row style="height:calc(100% - 50px);display:flex;position: relative;">
  11. <el-col style="height:100%;position: relative;" :span="this.leftwidth" >
  12. <div class="main-layout" v-on:mouseover="changeActive('3D')">
  13. <div id="3DcesiumContainer" class="map"></div>
  14. </div>
  15. </el-col>
  16. <el-col style="height:100%;position: relative;" :span="(24-this.leftwidth)" >
  17. <div class="main-layout" v-on:mouseover="changeActive('2D')">
  18. <div id="2DcesiumContainer" class="map"></div>
  19. </div>
  20. </el-col>
  21. <el-tabs type="border-card" v-model="activeName" tab-position="left" class="menu" :stretch="true" @tab-click="legendClick">
  22. <el-tab-pane name="first">
  23. <span slot="label">
  24. <el-tooltip class="item" effect="dark" content="标绘" placement="left">
  25. <i class="el-icon-wind-power"></i>
  26. </el-tooltip>
  27. </span>
  28. </el-tab-pane>
  29. <el-tab-pane name="second">
  30. <span slot="label">
  31. <el-tooltip class="item" effect="dark" content="模型" placement="left">
  32. <i class="el-icon-receiving"></i>
  33. </el-tooltip>
  34. </span>
  35. </el-tab-pane>
  36. </el-tabs>
  37. <div class="buttons">
  38. <el-button icon="el-icon-full-screen" circle @click="ismax()"></el-button>
  39. <el-button icon="el-icon-coin" circle></el-button>
  40. <el-button icon="el-icon-bangzhu" @click="Fly()" circle></el-button>
  41. <el-button icon="el-icon-house" circle @click="home()"></el-button>
  42. <el-button icon="el-icon-plus" circle @click="big()"></el-button>
  43. <el-button icon="el-icon-minus" circle @click="small()"></el-button>
  44. </div>
  45. </el-row>
  46. </div>
  47. </template>
  48. <script>
  49. import screenfull from 'screenfull';
  50. export default ({
  51. computed: {
  52. },
  53. data () {
  54. return {
  55. viewer2D: null,
  56. viewer3D: null,
  57. dimension: 3,
  58. leftwidth: 0,
  59. mousevalue: null,
  60. shouldAnimate: false,
  61. activeName: 'first',
  62. currentLab: {
  63. index: -1,
  64. isActive: false
  65. }
  66. }
  67. },
  68. mounted () {
  69. this.cesiumInit();
  70. },
  71. methods: {
  72. cesiumInit () {
  73. this.viewer2D = new this.Cesium.Viewer('2DcesiumContainer', {
  74. sceneMode: this.Cesium.SceneMode.SCENE2D,
  75. animation: false, // 是否显示时间轴动画
  76. baseLayerPicker: false,
  77. homeButton: false,
  78. geocoder: false,
  79. timeline: false,
  80. fullscreenButton: false,
  81. sceneModePicker: false,
  82. navigationHelpButton: false,
  83. selectionIndicator: false,
  84. imageryProvider: new this.Cesium.UrlTemplateImageryProvider({
  85. url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
  86. layer: 'tdtVecBasicLayer',
  87. style: 'default',
  88. format: 'image/png',
  89. tileMatrixSetID: 'GoogleMapsCompatible',
  90. show: false
  91. })
  92. });
  93. this.viewer3D = new this.Cesium.Viewer('3DcesiumContainer', {
  94. animation: false, // 是否显示时间轴动画
  95. baseLayerPicker: false,
  96. homeButton: false,
  97. geocoder: false,
  98. timeline: false,
  99. fullscreenButton: false,
  100. sceneModePicker: false,
  101. navigationHelpButton: false,
  102. selectionIndicator: false,
  103. // imageryProvider: new this.Cesium.WebMapServiceImageryProvider({
  104. // url: '/geoserver/map/wms',
  105. // // 这里是自定义的图层名称
  106. // layers: 'map',
  107. // parameters: {
  108. // service: 'WMS',
  109. // format: 'image/png',
  110. // transparent: true
  111. // }
  112. // }),
  113. imageryProvider:
  114. // new this.Cesium.WebMapTileServiceImageryProvider({
  115. // url: "http://10.170.16.95:8080/geoserver/gwc/service/wmts/rest/map:map/{style}/{TileMatrixSet}/{TileMatrixSet}:{TileMatrix}/{TileRow}/{TileCol}?format=image/png",
  116. // layer: 'map:map',
  117. // style: 'raster',
  118. // format: 'image/png',
  119. // tileMatrixSetID: 'EPSG:900913', //一般使用EPSG:3857坐标系
  120. // }),
  121. new this.Cesium.UrlTemplateImageryProvider({
  122. url: 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
  123. layer: 'tdtVecBasicLayer',
  124. style: 'default',
  125. format: 'image/png',
  126. tileMatrixSetID: 'GoogleMapsCompatible',
  127. show: false
  128. })
  129. });
  130. this.viewer3D._cesiumWidget._creditContainer.style.display = 'none';
  131. this.viewer2D._cesiumWidget._creditContainer.style.display = 'none';
  132. this.viewer3D.camera.setView({
  133. destination: new this.Cesium.Cartesian3.fromDegrees(117.918977, 25.0, 1500000)
  134. // 方向,俯视和仰视的视角
  135. // orientation:{
  136. // heading: this.Cesium.Math.toRadians(90),//坐标系旋转90度
  137. // pitch: this.Cesium.Math.toRadians(-45) ,//设置俯仰角度为-45度
  138. // }
  139. })
  140. this.viewer2D.camera.setView({
  141. destination: new this.Cesium.Cartesian3.fromDegrees(117.918977, 25.0, 1500000)
  142. // 方向,俯视和仰视的视角
  143. // orientation:{
  144. // heading: this.Cesium.Math.toRadians(90),//坐标系旋转90度
  145. // pitch: this.Cesium.Math.toRadians(-45) ,//设置俯仰角度为-45度
  146. // }
  147. })
  148. // this.viewer.scene.screenSpaceCameraController.maximumZoomDistance = 1500000; //相机高度的最大值
  149. this.viewer2D.camera.percentageChanged = 0.01;
  150. this.viewer2D.camera.changed.addEventListener(this.sync);
  151. this.viewer3D.camera.percentageChanged = 0.01;
  152. this.viewer3D.camera.changed.addEventListener(this.sync);
  153. },
  154. removeAllModel () {
  155. this.viewer.entities.removeAll(); // 移除全部模型
  156. },
  157. sync () {
  158. if (this.mousevalue == '3D') {
  159. // 三维地图中心点
  160. let center = new this.Cesium.Cartesian2(
  161. Math.floor(this.viewer3D.canvas.clientWidth / 2),
  162. Math.floor(this.viewer3D.canvas.clientHeight / 2)
  163. );
  164. // 转为世界坐标系
  165. let position = this.viewer3D.scene.camera.pickEllipsoid(center);
  166. console.log(position);
  167. // 判断中心点是否在椭球体上
  168. if (this.Cesium.defined(position)) {
  169. // 获取三维地图中心点与相机之间的距离
  170. let distance = this.Cesium.Cartesian3.distance(
  171. position,
  172. this.viewer3D.scene.camera.positionWC
  173. );
  174. position = this.convertWorldToCartographic(position)
  175. // 更新二维地图
  176. this.viewer2D.scene.camera.setView({
  177. destination: new this.Cesium.Cartesian3.fromDegrees(position.longitude, position.latitude, distance)
  178. })
  179. console.log(distance);
  180. }
  181. }
  182. if (this.mousevalue == '2D') {
  183. // 二维地图中心点
  184. let center = new this.Cesium.Cartesian2(
  185. Math.floor(this.viewer2D.canvas.clientWidth / 2),
  186. Math.floor(this.viewer2D.canvas.clientHeight / 2)
  187. );
  188. // 转为世界坐标系
  189. let position = this.viewer2D.scene.camera.pickEllipsoid(center);
  190. console.log(position);
  191. // 判断中心点是否在椭球体上
  192. if (this.Cesium.defined(position)) {
  193. // 获取三维地图中心点与相机之间的距离
  194. let distance = this.viewer2D.scene.camera.positionCartographic.height;
  195. position = this.convertWorldToCartographic(position)
  196. // 更新三维地图
  197. this.viewer3D.scene.camera.setView({
  198. destination: new this.Cesium.Cartesian3.fromDegrees(position.longitude, position.latitude, distance)
  199. })
  200. console.log(this.viewer2D.scene.camera.positionWC)
  201. }
  202. }
  203. },
  204. // 将世界坐标系转换为经纬度坐标系
  205. convertWorldToCartographic (worldPosition) {
  206. const ellipsoid = this.viewer2D.scene.globe.ellipsoid;
  207. const cartographic = ellipsoid.cartesianToCartographic(worldPosition);
  208. const longitude = this.Cesium.Math.toDegrees(cartographic.longitude);
  209. const latitude = this.Cesium.Math.toDegrees(cartographic.latitude);
  210. const height = cartographic.height;
  211. return { longitude, latitude, height };
  212. },
  213. // 监听地图变化
  214. changeActive (value) {
  215. this.mousevalue = value
  216. console.log(value)
  217. },
  218. dimensionswitch (value) {
  219. this.dimension = value
  220. if (value == 3) {
  221. this.leftwidth = 24;
  222. } else if (value == 2) {
  223. this.leftwidth = 0;
  224. } else {
  225. this.leftwidth = 12;
  226. }
  227. },
  228. // 全屏缩小
  229. ismax () {
  230. if (screenfull.isEnabled && screenfull.isFullscreen) {
  231. screenfull.exit();
  232. } else {
  233. screenfull.request();
  234. }
  235. },
  236. /* 获取camera中心点坐标 */
  237. getCenterPosition () {
  238. var result = this.viewer2D.camera.pickEllipsoid(new this.Cesium.Cartesian2(this.viewer2D.canvas.clientWidth / 2, this.viewer2D.canvas
  239. .clientHeight / 2))
  240. var curPosition = this.Cesium.Ellipsoid.WGS84.cartesianToCartographic(result)
  241. var lon = curPosition.longitude * 180 / Math.PI
  242. var lat = curPosition.latitude * 180 / Math.PI
  243. var height = this.viewer2D.camera.positionCartographic.height
  244. return {
  245. lon: lon,
  246. lat: lat,
  247. height: height
  248. }
  249. },
  250. /* 地图放大 */
  251. big () {
  252. // 镜头拉进
  253. this.viewer2D.camera.flyTo({
  254. destination: this.Cesium.Cartesian3.fromDegrees(this.getCenterPosition().lon, this.getCenterPosition().lat, this.getCenterPosition().height / 1.8),
  255. duration: 1.0
  256. })
  257. },
  258. /* 地图缩小 */
  259. small () {
  260. // 镜头远离
  261. this.viewer2D.camera.flyTo({
  262. destination: this.Cesium.Cartesian3.fromDegrees(this.getCenterPosition().lon, this.getCenterPosition().lat, this.getCenterPosition().height * 1.2),
  263. duration: 1.0
  264. })
  265. },
  266. home () {
  267. this.viewer2D.camera.flyTo({
  268. destination: this.Cesium.Cartesian3.fromDegrees(117.918977, 25.0, 1500000)
  269. });
  270. },
  271. Fly () {
  272. this.shouldAnimate = !this.shouldAnimate
  273. this.viewer2D.clock.shouldAnimate = true
  274. this.viewer3D.clock.shouldAnimate = true
  275. if (this.shouldAnimate == true) {
  276. this.startRun({ multiplier: 2000 })
  277. } else {
  278. this.stopRun()
  279. }
  280. },
  281. startRun (option = { multiplier: 1 }) {
  282. // 监听每次渲染前执行矩阵求解
  283. this.viewer3D.scene.postUpdate.addEventListener(this.rotateSetting)
  284. // 根据option修改一些参数
  285. if (this.viewer3D.clock) {
  286. const keys = Object.keys(option)
  287. for (let k of keys) {
  288. this.viewer3D.clock[k] = option[k]
  289. }
  290. }
  291. },
  292. stopRun () {
  293. let viewer = this.viewer3D
  294. viewer.clock.multiplier = 1
  295. viewer.scene.postUpdate.removeEventListener(this.rotateSetting)
  296. },
  297. rotateSetting () {
  298. let Cesium = this.Cesium
  299. let viewer = this.viewer3D
  300. if (!viewer || viewer.scene.mode !== Cesium.SceneMode.SCENE3D) {
  301. return
  302. }
  303. const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(
  304. viewer.clock.currentTime
  305. )
  306. // icrfToFixed 在上面的方法中,若没加载好所需的计算资源会返回undefined,判断下
  307. if (Cesium.defined(icrfToFixed)) {
  308. const camera = viewer.camera
  309. const offset = Cesium.Cartesian3.clone(camera.position)
  310. const transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed)
  311. camera.lookAtTransform(transform, offset)
  312. }
  313. },
  314. /// / tabs 切换点击
  315. legendClick (tab, event) {
  316. var tab_content = document.querySelectorAll('.menu .el-tabs__content');
  317. if (this.currentLab.index == tab.index) {
  318. tab_content[0].style.display == 'none' || '' ? tab_content[0].style.display = 'block' : tab_content[0].style.display = 'none';
  319. }
  320. if (this.currentLab.index != tab.index) {
  321. this.currentLab.index = tab.index;
  322. tab_content[0].style.display = 'block';
  323. }
  324. }
  325. }
  326. })
  327. </script>
  328. <style scoped>
  329. .header{
  330. background-color: #1c222b;
  331. color: #fff;
  332. height: 100%;
  333. display: flex;
  334. flex-direction: row;
  335. align-items:center;/*由于flex-direction: column,因此align-items代表的是水平方向*/
  336. justify-content: center;/*由于flex-direction: column,因此justify-content代表的是垂直方向*/
  337. }
  338. .header .el-button{
  339. width: 150px;
  340. margin: 0 10px;
  341. height:30px;
  342. padding: 1px 23px;
  343. }
  344. .box-card {
  345. min-height: 100%;
  346. height: 100%;
  347. }
  348. /deep/ .el-card__body {
  349. height: 100% !important;
  350. padding: 0px !important;
  351. }
  352. .main-layout {
  353. height: 100%;
  354. width: 100%;
  355. }
  356. .container {
  357. height: 100%;
  358. }
  359. .map {
  360. width: 100%;
  361. height: 100%;
  362. }
  363. .menu{
  364. position: absolute;
  365. height: 400px;
  366. z-index: 999;
  367. left: 20px;
  368. top: 20px;
  369. }
  370. /deep/.el-tabs--left .el-tabs__header.is-left {
  371. margin-right: unset !important;
  372. }
  373. .buttons{
  374. display: flex;
  375. flex-direction: column;
  376. align-items: center;
  377. justify-items: center;
  378. position: absolute;
  379. z-index: 999;
  380. right: 20px;
  381. top: 20px;
  382. }
  383. .el-button{
  384. margin: 5px !important;
  385. }
  386. </style>