nnnjjj123
2020-11-17 1b2c1edb61190eeb19f465ff031eaa3b2a1b8dbc
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
///////////////////////////////////////////////////////////////////////////
// Copyright © 2018 NarutoGIS. All Rights Reserved.
// 模块描述:鹰眼地图,和主地图进行联动
///////////////////////////////////////////////////////////////////////////
define([
        'dojo/_base/declare',
        'dojo/_base/lang',
        'dojo/_base/array',
        'dojo/_base/html',
        'dojo/topic',
        'jimu/BaseWidget'
    ],
    function (declare,
              lang,
              array,
              html,
              topic,
              BaseWidget
    ) {
 
        var CesiumViewTool = (function () {//http://blog.sina.com.cn/s/blog_15e866bbe0102y5no.html
            function _() {
            }
 
            _.GetViewExtent = function (viewer) {
                var extent = {};
                var scene = viewer.scene;
                var ellipsoid = scene.globe.ellipsoid;
                var canvas = scene.canvas;
 
                var car3_lt = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(0, 0), ellipsoid);// canvas左上角
                var car3_rb = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(canvas.width, canvas.height), ellipsoid); // canvas右下角
 
                // 当canvas左上角和右下角全部在椭球体上
                if (car3_lt && car3_rb) {
                    var carto_lt = ellipsoid.cartesianToCartographic(car3_lt);
                    var carto_rb = ellipsoid.cartesianToCartographic(car3_rb);
                    extent.xmin = Cesium.Math.toDegrees(carto_lt.longitude);
                    extent.ymax = Cesium.Math.toDegrees(carto_lt.latitude);
                    extent.xmax = Cesium.Math.toDegrees(carto_rb.longitude);
                    extent.ymin = Cesium.Math.toDegrees(carto_rb.latitude);
                } else if (!car3_lt && car3_rb) { // 当canvas左上角不在但右下角在椭球体上
                    return null;
                    var car3_lt2 = null;
                    var yIndex = 0;
                    var xIndex = 0;
                    do {
                        // 这里每次10像素递加,一是10像素相差不大,二是为了提高程序运行效率
                        yIndex <= canvas.height ? yIndex += 10 : canvas.height;
                        xIndex <= canvas.width ? xIndex += 10 : canvas.width;
                        car3_lt2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIndex, yIndex), ellipsoid);
                    } while (!car3_lt2);
                    var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt2);
                    var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb);
                    extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
                    extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
                    extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
                    extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
                }
                else if (car3_lt && !car3_rb) { // 当canvas左上角在但右下角不在椭球体上
                    return null;
                    var car3_rb2 = null;
                    var yIndex = canvas.height;
                    var xIndex = canvas.width;
                    do {
                        // 这里每次10像素递加,一是10像素相差不大,二是为了提高程序运行效率
                        yIndex >= 10 ? yIndex -= 10 : 10;
                        xIndex >= 10 ? xIndex -= 10 : 10;
                        car3_rb2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(yIndex, yIndex), ellipsoid);
                    } while (!car3_rb2);
                    var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt);
                    var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb2);
                    extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
                    extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
                    extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
                    extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
                } else if (!car3_lt && !car3_rb) {
                    return null;
                    var car3_lt2 = null;
                    var yIndex = 0;
                    var xIndex = 0;
                    do {
                        // 这里每次10像素递加,一是10像素相差不大,二是为了提高程序运行效率
                        yIndex <= canvas.height ? yIndex += 10 : canvas.height;
                        xIndex <= canvas.width ? xIndex += 10 : canvas.width;
                        car3_lt2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIndex, yIndex), ellipsoid);
                    } while (!car3_lt2);
 
                    var car3_rb2 = null;
                    var yIndex = canvas.height;
                    var xIndex = canvas.width;
                    do {
                        // 这里每次10像素递加,一是10像素相差不大,二是为了提高程序运行效率
                        yIndex >= 10 ? yIndex -= 10 : 10;
                        xIndex >= 10 ? xIndex -= 10 : 10;
                        car3_rb2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(yIndex, yIndex), ellipsoid);
                    } while (!car3_rb2);
 
                    var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt2);
                    var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb2);
                    extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
                    extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
                    extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
                    extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
                }
 
                // 获取高度
                extent.height = Math.ceil(viewer.camera.positionCartographic.height);
                return extent;
            }
 
            _.GetViewCenter = function (viewer) {
                var result = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(viewer.canvas.clientWidth / 2, viewer.canvas.clientHeight / 2));
                if (result) {
                } else {
                    return null;
                }
                var curPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(result);
                var lon = curPosition.longitude * 180 / Math.PI;
                var lat = curPosition.latitude * 180 / Math.PI;
                var height = viewer.camera.positionCartographic.height;
                return {
                    lgtd: lon,
                    lttd: lat,
                    height: height
                };
            }
            return _;
        })();
 
        return declare([BaseWidget], {
            baseClass: 'jimu-widget-EagleEye',
 
            layers:{},
            startup: function () {
                this.inherited(arguments);
                
                this.initEagleEye();
 
                var self = this;
                // // 定义当前场景的画布元素的事件处理
                // var handler = new Cesium.ScreenSpaceEventHandler(this.map.scene.canvas);
                // //设置鼠标移动事件的处理函数,这里负责监听x,y坐标值变化
                // handler.setInputAction(function(movement) {
                //     var extent = CesiumViewTool.GetViewExtent(window.viewer);
                //     self.createExtentPolygon([extent.xmin,extent.ymin],[extent.xmax,extent.ymax]);
                // }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
                // //设置鼠标滚动事件的处理函数,这里负责监听高度值变化
                // handler.setInputAction(function(wheelment) {
                //     var extent = CesiumViewTool.GetViewExtent(window.viewer);
                //     self.createExtentPolygon([extent.xmin,extent.ymin],[extent.xmax,extent.ymax]);
                // }, Cesium.ScreenSpaceEventType.WHEEL);
 
                var updateTimer;
                if (true) {
                    var camera = window.viewer.camera;
                    camera.moveStart.addEventListener(function() {
                        if (!Cesium.defined(updateTimer)) {
                            updateTimer = window.setInterval(self.extentChange, 1000);
                        }
                    });
                    camera.moveEnd.addEventListener(function() {
                        if (Cesium.defined(updateTimer)) {
                            window.clearInterval(updateTimer);
                            updateTimer = undefined;
                        }
                        self.extentChange();
                    });
                }
            },
 
            extentChange:function(){//改变之后
                var extent = CesiumViewTool.GetViewExtent(window.viewer);
                var center = CesiumViewTool.GetViewCenter(window.viewer);
                if(extent){
                    this.createExtentPolygon([extent.xmin,extent.ymin],[extent.xmax,extent.ymax],center);
                }else{
                    if(!center)return;
                    this.eagleEyemap.camera.setView({
                        destination : Cesium.Cartesian3.fromDegrees(center.lgtd, center.lttd, center.height*4)
                    });
                }
 
            },
 
            createExtentPolygon: function (pt1,pt2,center) {
 
                if(!center)return;
                var ringsArray = [];
                ringsArray.push(pt1[0], pt1[1]);
                ringsArray.push(pt2[0], pt1[1]);
                ringsArray.push(pt2[0], pt2[1]);
                ringsArray.push(pt1[0], pt2[1]);
                ringsArray.push(pt1[0], pt1[1]);
                if(this.bound){
                    this.bound.polygon.hierarchy = Cesium.Cartesian3.fromDegreesArray(ringsArray);
                    this.eagleEyemap.camera.setView({
                        destination : Cesium.Cartesian3.fromDegrees(center.lgtd, center.lttd, center.height*4)
                    });
 
                    // this.eagleEyemap.camera.flyTo({
                    //     destination: Cesium.Rectangle.fromDegrees(pt1[0],  pt1[1], pt2[0], pt2[1])
                    // });
                    return;
                }
                this.bound = this.eagleEyemap.entities.add({
                    name : 'polygon',
                    polygon : {
                        hierarchy : Cesium.Cartesian3.fromDegreesArray(ringsArray),
                        material : Cesium.Color.RED.withAlpha(0.5),
                        outline : true,
                        outlineColor : Cesium.Color.RED,
                        outlineWidth:2.0
                    }
                });
                this.eagleEyemap.camera.setView({
                    destination : Cesium.Cartesian3.fromDegrees(center.lgtd, center.lttd, center.height*4)
                });
                // this.eagleEyemap.camera.setView({
                //     destination : Cesium.Rectangle.fromDegrees(pt1[0],  pt1[1], pt2[0], pt2[1])
                // });
 
            },
            initEagleEye:function(){
                //添加鹰眼
                var layer = new Cesium.ArcGisMapServerImageryProvider({url:"http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineCommunity/MapServer"});
 
                this.eagleEyemap = new Cesium.Viewer(this.eagleEye,{
 
                    "animation":false,
                    "baseLayerPicker":false,
                    "fullscreenButton":false,
                    "geocoder":false,
                    "homeButton":false,
                    "infoBox" : false,
                    "sceneModePicker":false,
                    "selectionIndicator" : false ,
                    "timeline":false,
                    "navigationHelpButton":false,
                    "scene3DOnly" : false,
                    "navigationInstructionsInitiallyVisible":false,
                    "terrainExaggeration":1,
                    "showRenderLoopErrors":false,
                    "sceneMode":Cesium.SceneMode.SCENE2D,
                    "imageryProvider":layer
                });
                //去掉版权
                this.eagleEyemap._cesiumWidget._creditContainer.style.display = "none";
 
 
                var extent = {
                    "xmin": 86.06689524994869,
                    "ymin": 22.108857182532834,
                    "xmax": 124.9584857499487,
                    "ymax": 48.08052948253283};
 
                var west = extent.xmin;
                var south = extent.ymin;
                var east =extent.xmax;
                var north =extent.ymax;
 
                this.eagleEyemap.camera.setView({
                    destination : Cesium.Rectangle.fromDegrees(west, south, east, north)
                });
            },
            onOpen: function () {
                //面板打开的时候触发 (when open this panel trigger)
            },
 
            onClose: function () {
                //面板关闭的时候触发 (when this panel is closed trigger)
            },
 
            onMinimize: function () {
                this.resize();
            },
 
            onMaximize: function () {
                this.resize();
            },
 
            resize: function () {
 
            },
 
            destroy: function () {
                //销毁的时候触发
                //todo
                //do something before this func
                this.inherited(arguments);
            },
            flag:true,
            vis:function(){
               if(this.flag){
                   $(this.eagleEye).hide();
                   this.visNode.innerHTML = "显示";
 
                   $(this.domNode).height(30);
                   $(this.domNode).width(50);
               }else{
                   $(this.eagleEye).show();
                   $(this.domNode).height(this.position.height);
                   $(this.domNode).width(this.position.width);
                   this.visNode.innerHTML = "隐藏";
               }
 
                this.flag = !this.flag;
            }
 
        });
    });