北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
lixuliang
2024-05-08 f5233339920a59103d53e8efadc6f3f5d0b64f31
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
/*
*cesium组件封装
*20190307
 */
export default{
  install(Vue){
    /*
    *导航属性
     */
    Vue.prototype.CesiumAPI = {
      /*
      *导航接口
       */
      NavigateObj: {
        /*
        *飞到当前位置
       *参数x:经度
       *参数y:纬度
       *参数h:高度
       */
        flyToXYZ: (Cesium, Viewerc, x, y, z) => {
          Viewerc.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(x, y, z)
          });
        },
        /*
        *放大 视角拉近
       */
        zoomIn: (Viewerc) => {
          Viewerc.camera.zoomIn((1000000))
        },
        /*
        *缩小 视角拉远
       */
        zoomOut: (Viewerc) => {
          Viewerc.camera.zoomOut((1000000))
        },
        /*
        *跳转
        * 参数:Viewerc,Cesium,obj
         */
        JumpTo: (Viewerc, Cesium, obj) => {
          if (obj.position != null && obj.position != undefined) {
            Viewerc.camera.setView({
              destination: obj.position,
              orientation: {
                heading: Cesium.Math.toRadians(0),
                pitch: Cesium.Math.toRadians(-45.0),
                roll: 0.0
              }
            });
          }
        }
      },
      /*
      *创建接口
       */
      CreateObj: {
        /*
        *创建点
        *参数point:必选项 点的属性 {
            position: cartesian,
            point: {
              color: Cesium.Color.RED,
              pixelSize: 5,
              heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
            }
          };
         */
        createPoint: (tag, Viewerc, point, callback) => {
          var temp = _isContain(createobjlist, tag);
          if (temp.index > -1) {
            Viewerc.entities.remove(temp.item);
            createobjlist.splice(temp.index, 1);
          } else {
            var en = Viewerc.entities.add(point);
            createobjlist.push({tag: tag, item: en});
            if (typeof callback == 'function') {
              callback({tag: tag, item: en, point: point});
            }
          }
        },
        /*
        *功能:创建线 左键开始 右键结束 创建
        *参数
         */
        createLine: (tag, Viewerc, Cesium, handler, callback) => {
          var createLine;
          var temp = _isContain(createobjlist, tag);
          if (temp.index > -1) {
            Viewerc.entities.remove(temp.item);
            createobjlist.splice(temp.index, 1);
          } else {
 
            var PolyLinePrimitive = (function () {
              function _(positions) {
                this.options = {
                  polyline: {
                    show: true,
                    positions: [],
                    material: Cesium.Color.RED,
                    width: 3
                  }
                };
                this.positions = positions;
                this._init();
              }
 
              _.prototype._init = function () {
                var _self = this;
                var _update = function () {
                  return _self.positions;
                };
                //实时更新polyline.positions
                this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
                createLine = Viewerc.entities.add(this.options);
 
              };
              return _;
            })();
            var positions = [];
            var poly = undefined;
            //鼠标左键单击画点
            handler.setInputAction(function (movement) {
              var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.position, Viewerc.scene.globe.ellipsoid);
              if (positions.length == 0) {
                positions.push(cartesian.clone());
              }
              positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //鼠标移动
            handler.setInputAction(function (movement) {
              var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.endPosition, Viewerc.scene.globe.ellipsoid);
              if (positions.length >= 2) {
                if (!Cesium.defined(poly)) {
                  poly = new PolyLinePrimitive(positions);
                } else {
                  if (cartesian != undefined) {
                    positions.pop();
                    cartesian.y += (1 + Math.random());
                    positions.push(cartesian);
                  }
                }
              }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            //单击鼠标右键结束画线
            handler.setInputAction(function (movement) {
              handler.destroy();
              new PolyLinePrimitive(positions);
              if (typeof callback == 'function') {
                callback({tag: tag, item: createLine});
              }
              createobjlist.push({tag: tag, item: createLine});
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
          }
 
        },
        /*
        *功能:画面 左键开始 右键结束
         */
        createPolygon: (tag, Viewerc, Cesium, handler, callback) => {
          var temp = _isContain(createobjlist, tag);
          if (temp.index > -1) {
            Viewerc.entities.remove(temp.item);
            createobjlist.splice(temp.index, 1);
          } else {
            var Polygon;
            var PolygonPrimitive = (function () {
              function _(positions) {
                this.options = {
                  name: '多边形',
                  polygon: {
                    hierarchy: [],
                    perPositionHeight: true,
                    material: Cesium.Color.RED.withAlpha(0.4)
                  }
                };
                this.hierarchy = positions;
                this._init();
              }
 
              _.prototype._init = function () {
                var _self = this;
                var _update = function () {
                  return _self.hierarchy;
                };
                //实时更新polygon.hierarchy
                this.options.polygon.hierarchy = new Cesium.CallbackProperty(_update, false);
                Polygon = Viewerc.entities.add(this.options);
              };
              return _;
            })();
 
            var positions = [];
            var poly = undefined;
 
            //鼠标单击画点
            handler.setInputAction(function (movement) {
              var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.position, Viewerc.scene.globe.ellipsoid);
              if (positions.length == 0) {
                positions.push(cartesian.clone());
              }
              positions.push(cartesian);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //鼠标移动
            handler.setInputAction(function (movement) {
              var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.endPosition, Viewerc.scene.globe.ellipsoid);
              if (positions.length >= 2) {
                if (!Cesium.defined(poly)) {
                  poly = new PolygonPrimitive(positions);
                } else {
                  if (cartesian != undefined) {
                    positions.pop();
                    cartesian.y += (1 + Math.random());
                    positions.push(cartesian);
                  }
                }
              }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            //鼠标右键单击结束绘制
            handler.setInputAction(function (movement) {
              handler.destroy();
              new PolygonPrimitive(positions);
              if (typeof callback == 'function') {
                callback({tag: tag, item: Polygon});
              }
              createobjlist.push({tag: tag, item: Polygon});
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
          }
        },
        /*画矩形 左键开始 右键结束*/
        drawRect: (tag, _this,Viewerc, Cesium, handle, callback) => {
          var temp = _isContain(createobjlist, tag);
          if (temp.index > -1) {
            Viewerc.entities.remove(temp.item);
            createobjlist.splice(temp.index, 1);
          } else {
            var _self = _this;
            var pointsArr = [];
            _self.shape = {
              points: [],
              rect: null,
              entity: null
            };
            var tempPosition;
            /*var handle = new Cesium.ScreenSpaceEventHandler(_self.viewer.scene.canvas);*/
            //鼠标左键单击画点
            handle.setInputAction(function (click) {
              tempPosition = _self.getPointFromWindowPoint(click.position);
              //选择的点在球面上
              if (tempPosition) {
                if (_self.shape.points.length == 0) {
                  pointsArr.push(tempPosition);
                  _self.shape.points.push(_self.viewer.scene.globe.ellipsoid.cartesianToCartographic(tempPosition));
                  _self.shape.rect = Cesium.Rectangle.fromCartographicArray(_self.shape.points);
                  _self.shape.rect.east += 0.000001;
                  _self.shape.rect.north += 0.000001;
                  _self.shape.entity = _self.viewer.entities.add({
                    rectangle: {
                      coordinates: _self.shape.rect,
                      material: Cesium.Color.BLACK.withAlpha(0.4),
                      outline: true,
                      outlineWidth: 2,
                      outlineColor: Cesium.Color.RED,
                      height: 0
                    }
                  });
                  _self.bufferEntity = _self.shape.entity;
                } else {
                  handle.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                  handle.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
                  callback(pointsArr);
                }
              }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //鼠标移动
            handle.setInputAction(function (movement) {
              if (_self.shape.points.length == 0) {
                return;
              }
              var moveEndPosition = _self.getPointFromWindowPoint(movement.endPosition);
              //选择的点在球面上
              if (moveEndPosition) {
                pointsArr[1] = moveEndPosition;
                _self.shape.points[1] = _self.viewer.scene.globe.ellipsoid.cartesianToCartographic(moveEndPosition);
                _self.shape.rect = Cesium.Rectangle.fromCartographicArray(_self.shape.points);
                if (_self.shape.rect.west == _self.shape.rect.east)
                  _self.shape.rect.east += 0.000001;
                if (_self.shape.rect.south == _self.shape.rect.north)
                  _self.shape.rect.north += 0.000001;
                _self.shape.entity.rectangle.coordinates = _self.shape.rect;
              }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
          }
        }
      }
    }
  }
}
/*
*判断当前数组是否存在该对象
 */
window._isContain = function(layerlist, tag) {
  for (var i = 0; i < layerlist.length; i++) {
    var temp = layerlist[i];
    if (temp.tag == tag) {
      temp.index = i;
      return temp;
    }
  }
  return { item: null, index: -1 };
}
window.createobjlist = [];