在main.js引入

import Cesium from 'cesium/Cesium'
import '../node_modules/cesium/Build/Cesium/Widgets/widgets.css'

Vue.prototype.Cesium = Cesium;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<br>// 以下是组件内容<br><br><br><br><template>
    <div class="cesium-wrap">
        <div id="cesiumContainer"></div>
        <el-form ref="form" :model="form" label-width="80px" class="cesiumForm" v-if="showForm" size="mini">
            <el-form-item label="id">
                <el-select v-model="form.id" placeholder="请选择" @change="changeLing">
                    <el-option v-for="item in lingList" :key="'ling' + item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名称"><el-input v-model="form.name"></el-input></el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit()">提交</el-button>
                <el-button @click="handleClose">取消</el-button>
                <el-button @click="deleHandle">删除</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
 
<script>
export default {
    data() {
        return {
            terrainUrl: 'http://172.0.0.1:8888/terrain2/',
            imageryUrl: 'http://172.0.0.1:8888/image/{z}/{x}/{y}.jpg',
            orgPos: { lon: 99.79602474, lat: 26.45798084, height: 300000 }, //7286.13}
            viewer: null, //
            terrainProvider: '', // 添加地形图数据
            imageryProvider: '', // 图形数据
            form: {
                id: '',
                name: '',
                jd: '',
                wd: '',
                height: ''
            },
            showForm: false,
            img: '/static/images/mark.png',
            lingList: []
        };
    },
    created() {
        this.queryMingTombsHandler();
    },
    mounted() {
        this.initViewer();
    },
    watch: {},
    methods: {
        // 获取数据
        queryMingTombsHandler() {
            this.lingList = [];
            this.$commonHttp(请求地址).then(res => {
                if (res.code == this.$codeState.OKCODE) {
                    let data = res.data;
                    this.lingList = data;
                    this.lingList.forEach(item => {
                        if (item.height) {
                            this.addEntities(item);
                        }
                    });
                    console.log('lingList', this.lingList);
                }
            });
        },
        // 选择
        changeLing(val) {
            this.lingList.forEach(item => {
                if (item.id == val) {
                    this.form.name = item.name;
                    console.log('this.form', this.form);
                }
            });
        },
        onSubmit() {
            this.$commonHttp(请求地址, {
                id: this.form.id,
                jd: this.form.jd,
                wd: this.form.wd,
                height: this.form.height
            }).then(res => {
                if (res.code == this.$codeState.OKCODE) {
                    this.addEntities(this.form);
                    this.showForm = false;
                    this.clearFromHandler();
                }
            });
        },
        handleClose() {
            this.showForm = false;
        },
        deleHandle() {
            this.$commonHttp(请求地址, {
                id: this.form.id
            }).then(res => {
                if (res.code == this.$codeState.OKCODE) {
                    this.showForm = false;
                    this.clearFromHandler();
                }
            });
        },
        // 将屏幕坐标转为经纬度
        lonLan(position) {
            let viewer = this.viewer;
            let Cesium = this.Cesium;
            var position1, cartographic1, lon, lat, height;
            let lonLanHei = {
                lon: '',
                lat: '',
                height: ''
            };
            let ray = viewer.scene.camera.getPickRay(position);
            position1 = viewer.scene.globe.pick(ray, viewer.scene);
            cartographic1 = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
            let feature = viewer.scene.pick(position);
            if (feature == undefined) {
                lon = Cesium.Math.toDegrees(cartographic1.longitude);
                lat = Cesium.Math.toDegrees(cartographic1.latitude);
                height = cartographic1.height;
            } else {
                let cartesian = viewer.scene.pickPosition(position);
                if (Cesium.defined(cartesian)) {
                    let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                    lon = Cesium.Math.toDegrees(cartographic.longitude);
                    lat = Cesium.Math.toDegrees(cartographic.latitude);
                    height = cartographic.height; //模型高度 } };//
                }
            }
            lonLanHei.lat = lat;
            lonLanHei.lon = lon;
            lonLanHei.height = height;
            return lonLanHei;
        },
        clearFromHandler() {
            this.form.id = '';
            this.form.jd = '';
            this.form.wd = '';
            this.form.name = '';
            this.form.height = '';
        },
 
        initViewer() {
            //添加地形图数据
            // this.terrainProvider = new this.Cesium.CesiumTerrainProvider({
            //   url: this.terrainUrl
            // });
            // 添加图片数据
            // this.imageryProvider = new this.Cesium.UrlTemplateImageryProvider({
            //   url: this.imageryUrl,
            //   tilingScheme: new this.Cesium.WebMercatorTilingScheme(),
            //   maximumLevel: 19
            // });
            let terrainModels = this.Cesium.createDefaultTerrainProviderViewModels();
            // 初始化
            this.viewer = new this.Cesium.Viewer('cesiumContainer', {
                // terrainProvider: this.terrainProvider,
                // imageryProvider: this.imageryProvider,
                infoBox: false, //是否显示信息框--点击mark时,true有会
                geocoder: false, // 查找位置,查到后会将镜头对准找到的地址
                homeButton: false, // 视角返回初始位置
                sceneModePicker: false, // 旋转视角的模式,有三种3D,2D,哥伦布视图
                //baseLayerPicker: false, // 图层选择器,选择要显示的地图服务和地形服务。
                navigationHelpButton: false, // 导航帮助按钮,显示默认的地图控制帮助。
                fullscreenButton: true, // 全屏按钮
                animation: false, // 动画,控制试图动画的播放速度
                timeline: false, // 时间线,指示当前时间,并允许用户跳到特定的时间。
                selectionIndicator: false, // 禁止entity选中
                vrButton: true,
                scene3DOnly: false,
                sceneMode: 3
                //terrainProviderViewModels: terrainModels,
                //selectedTerrainProviderViewModel: terrainModels[1] // Select STK high-res terrain
            });
            this.viewer.scene.globe.depthTestAgainstTerrain = false;
            // cesium的label的清晰度
            this.viewer.scene.postProcessStages.fxaa.enabled = false;
            // 加载倾斜摄影数据
            this.loadTilesetHandler();
            // this.initCamera();
            //去掉地图标志
            this.viewer._cesiumWidget._creditContainer.style.display = 'none';
            this.initMark();
        },
        // 加载倾斜摄影数据
        loadTilesetHandler() {
            let tileset = this.viewer.scene.primitives.add(
                new this.Cesium.Cesium3DTileset({
                    url: 'http://172.18.49.225:65432/qxsy/tileset.json' //
                })
            );
            tileset.readyPromise
                .then(() => {
                    this.viewer.scene.primitives.lowerToBottom(tileset);
                    let boundingSphere = tileset.boundingSphere;
                    this.viewer.camera.viewBoundingSphere(boundingSphere, new this.Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
                    this.viewer.camera.lookAtTransform(this.Cesium.Matrix4.IDENTITY);
                })
                .otherwise(function(Aerror) {
                    throw error;
                });
        },
        initCamera() {
            let self = this;
            this.viewer.scene.camera.flyTo({
                destination: this.Cesium.Cartesian3.fromDegrees(this.orgPos.lon, this.orgPos.lat, this.orgPos.height), // 设置位置
                orientation: {
                    heading: this.Cesium.Math.toRadians(0.0), // 方向
                    pitch: this.Cesium.Math.toRadians(-90), // 倾斜角度
                    roll: this.Cesium.Math.toRadians(0)
                },
                complete: () => {
                    // 到达位置后执行的回调函数
                    this.viewer.scene.camera.flyTo({
                        destination: self.Cesium.Cartesian3.fromDegrees(self.orgPos.lon, self.orgPos.lat, self.orgPos.height),
                        orientation: {
                            heading: self.Cesium.Math.toRadians(0.0),
                            pitch: self.Cesium.Math.toRadians(-30),
                            roll: self.Cesium.Math.toRadians(0)
                        },
                        complete: () => {},
                        duration: 3
                    });
                },
                duration: 5 // 设置飞行持续时间,默认会根据距离来计算
            });
        },
        initMark() {
            const viewer = this.viewer;
            const Cesium = this.Cesium;
 
            // 左击进入360全景
            viewer.screenSpaceEventHandler.setInputAction(e => {
                viewer.selectedEntity = this.pickEntity(viewer, e.position);
                if (!viewer.selectedEntity) {
                    let obj = this.lonLan(e.position);
                    this.form.jd = obj.lon;
                    this.form.wd = obj.lat;
                    this.form.height = obj.height;
                    this.showForm = true;
                } else {
                    this.$parent.detailInfo(viewer.selectedEntity._id);
                    console.log('进入360全景', viewer.selectedEntity._id);
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
 
            // 双击entity,会放大视图,entity居中显示,且鼠标左键失去移动功能,鼠标滚轮失去作用
            this.viewer.screenSpaceEventHandler.setInputAction(() => {}, this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        },
        //双击操作
        pickAndTrackObject(e) {},
        // 设置广告牌label
        addEntities(item) {
            const viewer = this.viewer;
            const Cesium = this.Cesium;
            // label显示清晰度
            viewer.scene.postProcessStages.fxaa.enabled = false;
            let wyoming = viewer.entities.add({
                id: item.id,
                name: item.name,
                position: Cesium.Cartesian3.fromDegrees(Number(item.jd), Number(item.wd), Number(item.height) + 10),
                //立广告牌
                billboard: {
                    image: this.img,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    show: true, // default
                    width: 50, // default: undefined
                    height: 55
                },
                //字体标签样式
                label: {
                    text: item.name,
                    font:'normal 32px MicroSoft YaHei',
                    scale:0.5,
                    color: Cesium.Color.WHITE,
                    style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: true,
                    horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                    verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                    // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(10.0, 8000.0),
                    // disableDepthTestDistance: 100.0,
                    pixelOffset: new Cesium.Cartesian2(40, -20) // 调整偏移位置
                }
            });
        },
        // 拾取实体
        pickEntity(viewer, position) {
            const Cesium = this.Cesium;
            let picked = viewer.scene.pick(position);
            if (picked) {
                let id = Cesium.defaultValue(picked.id, picked.primitive.id);
                if (id instanceof Cesium.Entity) {
                    return id;
                }
            }
            return undefined;
        }
    }
};
</script>
 
<style scoped lang="less">
.cesium-wrap {
    width: 100%;
    height: 100%;
    display: inline-block;
}
#cesiumContainer {
    width: 100%;
    height: 100%;
    position: absolute;
    margin: 0;
    padding: 0;
    overflow: hidden;
}
.cesiumForm {
    width: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 10;
    background-color: #ccc;
    padding: 10px;
    transform: translate(-50%, -50%);
}
</style>