index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. convertWorldToCartographic(worldPosition) {
  205. const ellipsoid = this.viewer2D.scene.globe.ellipsoid;
  206. const cartographic = ellipsoid.cartesianToCartographic(worldPosition);
  207. const longitude = this.Cesium.Math.toDegrees(cartographic.longitude);
  208. const latitude = this.Cesium.Math.toDegrees(cartographic.latitude);
  209. const height = cartographic.height;
  210. return { longitude, latitude, height };
  211. },
  212. // 监听地图变化
  213. changeActive(value){
  214. this.mousevalue = value
  215. console.log(value)
  216. },
  217. dimensionswitch(value){
  218. this.dimension = value
  219. if(value == 3) {
  220. this.leftwidth = 24;
  221. }else if(value == 2){
  222. this.leftwidth = 0;
  223. }else{
  224. this.leftwidth = 12;
  225. }
  226. },
  227. //全屏缩小
  228. ismax(){
  229. if(screenfull.isEnabled && screenfull.isFullscreen){
  230. screenfull.exit();
  231. }else{
  232. screenfull.request();
  233. }
  234. },
  235. /* 获取camera中心点坐标 */
  236. getCenterPosition () {
  237. var result = this.viewer2D.camera.pickEllipsoid(new this.Cesium.Cartesian2(this.viewer2D.canvas.clientWidth / 2, this.viewer2D.canvas
  238. .clientHeight / 2))
  239. var curPosition = this.Cesium.Ellipsoid.WGS84.cartesianToCartographic(result)
  240. var lon = curPosition.longitude * 180 / Math.PI
  241. var lat = curPosition.latitude * 180 / Math.PI
  242. var height = this.viewer2D.camera.positionCartographic.height
  243. return {
  244. lon: lon,
  245. lat: lat,
  246. height: height
  247. }
  248. },
  249. /* 地图放大 */
  250. big () {
  251. // 镜头拉进
  252. this.viewer2D.camera.flyTo({
  253. destination: this.Cesium.Cartesian3.fromDegrees(this.getCenterPosition().lon, this.getCenterPosition().lat, this.getCenterPosition().height / 1.8),
  254. duration: 1.0
  255. })
  256. },
  257. /* 地图缩小 */
  258. small () {
  259. // 镜头远离
  260. this.viewer2D.camera.flyTo({
  261. destination: this.Cesium.Cartesian3.fromDegrees(this.getCenterPosition().lon, this.getCenterPosition().lat, this.getCenterPosition().height * 1.2),
  262. duration: 1.0
  263. })
  264. },
  265. home () {
  266. this.viewer2D.camera.flyTo({
  267. destination: this.Cesium.Cartesian3.fromDegrees(117.918977,25.0,1500000)
  268. });
  269. },
  270. Fly() {
  271. this.shouldAnimate = !this.shouldAnimate
  272. this.viewer2D.clock.shouldAnimate = true
  273. this.viewer3D.clock.shouldAnimate = true
  274. if(this.shouldAnimate == true){
  275. this.startRun({ multiplier: 2000 })
  276. }else{
  277. this.stopRun()
  278. }
  279. },
  280. startRun (option = { multiplier: 1 }) {
  281. // 监听每次渲染前执行矩阵求解
  282. this.viewer3D.scene.postUpdate.addEventListener(this.rotateSetting)
  283. // 根据option修改一些参数
  284. if (this.viewer3D.clock) {
  285. const keys = Object.keys(option)
  286. for (let k of keys) {
  287. this.viewer3D.clock[k] = option[k]
  288. }
  289. }
  290. },
  291. stopRun () {
  292. let viewer = this.viewer3D
  293. viewer.clock.multiplier = 1
  294. viewer.scene.postUpdate.removeEventListener(this.rotateSetting)
  295. },
  296. rotateSetting () {
  297. let Cesium = this.Cesium
  298. let viewer = this.viewer3D
  299. if (!viewer || viewer.scene.mode !== Cesium.SceneMode.SCENE3D) {
  300. return
  301. }
  302. const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(
  303. viewer.clock.currentTime
  304. )
  305. // icrfToFixed 在上面的方法中,若没加载好所需的计算资源会返回undefined,判断下
  306. if (Cesium.defined(icrfToFixed)) {
  307. const camera = viewer.camera
  308. const offset = Cesium.Cartesian3.clone(camera.position)
  309. const transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed)
  310. camera.lookAtTransform(transform, offset)
  311. }
  312. },
  313. //// tabs 切换点击
  314. legendClick(tab, event) {
  315. var tab_content = document.querySelectorAll(".menu .el-tabs__content");
  316. if (this.currentLab.index == tab.index) {
  317. tab_content[0].style.display == 'none'|| ''? tab_content[0].style.display = 'block' : tab_content[0].style.display = 'none';
  318. }
  319. if (this.currentLab.index != tab.index) {
  320. this.currentLab.index = tab.index;
  321. tab_content[0].style.display = 'block';
  322. }
  323. }
  324. }
  325. })
  326. </script>
  327. <style scoped>
  328. .header{
  329. background-color: #1c222b;
  330. color: #fff;
  331. height: 100%;
  332. display: flex;
  333. flex-direction: row;
  334. align-items:center;/*由于flex-direction: column,因此align-items代表的是水平方向*/
  335. justify-content: center;/*由于flex-direction: column,因此justify-content代表的是垂直方向*/
  336. }
  337. .header .el-button{
  338. width: 150px;
  339. margin: 0 10px;
  340. height:30px;
  341. padding: 1px 23px;
  342. }
  343. .box-card {
  344. min-height: 100%;
  345. height: 100%;
  346. }
  347. /deep/ .el-card__body {
  348. height: 100% !important;
  349. padding: 0px !important;
  350. }
  351. .main-layout {
  352. height: 100%;
  353. width: 100%;
  354. }
  355. .container {
  356. height: 100%;
  357. }
  358. .map {
  359. width: 100%;
  360. height: 100%;
  361. }
  362. .menu{
  363. position: absolute;
  364. height: 400px;
  365. z-index: 999;
  366. left: 20px;
  367. top: 20px;
  368. }
  369. /deep/.el-tabs--left .el-tabs__header.is-left {
  370. margin-right: unset !important;
  371. }
  372. .buttons{
  373. display: flex;
  374. flex-direction: column;
  375. align-items: center;
  376. justify-items: center;
  377. position: absolute;
  378. z-index: 999;
  379. right: 20px;
  380. top: 20px;
  381. }
  382. .el-button{
  383. margin: 5px !important;
  384. }
  385. </style>