ansel0926
2022-05-18 f0efddd9e32b9c5dc828401c4092be004b4826cf
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//from cdxiaodao cesium.js
//measure or survey point, height, distance, area
(function (window) {
    'use strict';
    
    function define_CesiumSurvey() {
        //Main object
        var CesiumSurvey = {};
        var handler;
        
        //Get 3D position String when left_click
        CesiumSurvey.getPosition = function(viewer){
            handler = handler && handler.destroy();
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            var resultStr;
            handler.setInputAction(function(movement){
                var cp = viewer.scene.pickPosition(movement.position);
                var pickedPositionString = cp.toString();
                //获取地理坐标(经度、纬度、高程)
                var cartographic = Cesium.Cartographic.fromCartesian(cp);
                var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
                var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
                var heightString = cartographic.height;
                var getResult = pickedPositionString+'\r\n'+
                    '======================'+'\r\n'+
                    '经度:'+longitudeString+'\r\n'+
                    '纬度:'+latitudeString+'\r\n'+
                    '高程:'+heightString+'\r\n'+
                    '======================'+'\r\n';
                resultStr = longitudeString+","+latitudeString+","+heightString;
                alert(getResult);
            },Cesium.ScreenSpaceEventType.LEFT_CLICK);
            handler.setInputAction(function(){
                handler = handler && handler.destroy();
            },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return resultStr;
        };
        
        //Get height String from 3dtiles to Mouse's position
        CesiumSurvey.measureHeight = function (viewer){
            handler = handler && handler.destroy();
            if((viewer.entities.getById('measureHeightLineEntity')!==undefined)){
                viewer.entities.removeById('measureHeightLineEntity');
            }
            if((viewer.entities.getById('heightPlaneEntity')!==undefined)){
                viewer.entities.removeById('heightPlaneEntity');
            }
            if((viewer.entities.getById('heightLabelEntity')!==undefined)){
                viewer.entities.removeById('heightLabelEntity');
            }
            var isFirst = true;
            var firstPosition;
            var secondPosition;
            var previousPosition;
            var heightLineEntity = viewer.entities.add({
                id : 'measureHeightLineEntity',
                polyline : {
                    followSurface : false,
                    width : 3,
                    material : Cesium.Color.RED,
                    show : true
                }
            });
            var heightPlaneEntity = viewer.entities.add({
                id : 'heightPlaneEntity',
                ellipse : {
                    outline : true,
                    show : false
                }
            });
            var heightLabelEntity = viewer.entities.add({
                id : 'heightLabelEntity',
                label : {
                    show : false,
                    verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                    horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
                    font : '16px Helvetica',
                    fillColor : Cesium.Color.YELLOW,
                    outlineColor : Cesium.Color.BLACK,
                    outlineWidth : 1,
                    style : Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: true,
                    backgroundColor: new Cesium.Color(0, 0, 1, 0.8),
                }
            });
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            handler.setInputAction(function(movement){
                if(isFirst){
                    firstPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(firstPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        firstPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    isFirst = false;
                }else{
                    secondPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(secondPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        secondPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    if(secondPosition){
                        previousPosition = secondPosition.clone();
                    }else{
                        secondPosition = previousPosition.clone();
                    }
                    var centerPosition = new Cesium.Cartesian3(
                        firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition)
                    );
                    heightLineEntity.polyline.positions = [firstPosition,centerPosition];
                    heightPlaneEntity.position = centerPosition;
                    var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition);
                    var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition);
                    heightPlaneEntity.ellipse.height = cartographicCenter.height;
                    heightPlaneEntity.ellipse.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.YELLOW.withAlpha(0.5):Cesium.Color.DEEPPINK.withAlpha(0.2);
                    heightPlaneEntity.ellipse.semiMinorAxis = Cesium.Cartesian3.distance(centerPosition,secondPosition);
                    heightPlaneEntity.ellipse.semiMajorAxis = heightPlaneEntity.ellipse.semiMinorAxis;
                    heightPlaneEntity.ellipse.show = true;
                    heightLabelEntity.position = (cartographicCenter.height>cartographicFirst.height)>0?centerPosition:firstPosition;
                    heightLabelEntity.label.show = true;
                    resultStr = cartographicCenter.height-cartographicFirst.height;
                    heightLabelEntity.label.text = '高度:'+resultStr.toFixed(2)+'米';
                    var camera = viewer.scene.camera;
                    heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0);
                    handler = handler && handler.destroy();
                }
            },Cesium.ScreenSpaceEventType.LEFT_CLICK);
            var resultStr;
            handler.setInputAction(function(movement){
                if(!isFirst){
                    secondPosition = viewer.scene.pickPosition(movement.endPosition);
                    var cartographic = Cesium.Cartographic.fromCartesian(secondPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.endPosition);
                        secondPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    if(secondPosition){
                        previousPosition = secondPosition.clone();
                    }else{
                        secondPosition = previousPosition.clone();
                    }
                    var centerPosition = new Cesium.Cartesian3(
                        firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition)
                    );
                    heightLineEntity.polyline.positions = [firstPosition,centerPosition];
                    heightPlaneEntity.position = centerPosition;
                    var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition);
                    var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition);
                    heightPlaneEntity.ellipse.height = cartographicCenter.height;
                    heightPlaneEntity.ellipse.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.YELLOW.withAlpha(0.5):Cesium.Color.DEEPPINK.withAlpha(0.2);
                    heightPlaneEntity.ellipse.semiMinorAxis = Cesium.Cartesian3.distance(centerPosition,secondPosition);
                    heightPlaneEntity.ellipse.semiMajorAxis = heightPlaneEntity.ellipse.semiMinorAxis;
                    heightPlaneEntity.ellipse.show = true;
                    heightLabelEntity.position = (cartographicCenter.height>cartographicFirst.height)>0?centerPosition:firstPosition;
                    heightLabelEntity.label.show = true;
                    resultStr = cartographicCenter.height-cartographicFirst.height;
                    heightLabelEntity.label.text = '高度:'+resultStr.toFixed(2)+'米';
                    var camera = viewer.scene.camera;
                    heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0);
                }
            },Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            handler.setInputAction(function(){
                handler = handler && handler.destroy();
            },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return resultStr;
        };
        
        //Get height String from 3dtiles to Mouse's position(三角尺)
        CesiumSurvey.measureHeightAngle = function (viewer){
            handler = handler && handler.destroy();
            if((viewer.entities.getById('measureHeightAngleLineEntity')!==undefined)){
                viewer.entities.removeById('measureHeightAngleLineEntity');
            }
            if((viewer.entities.getById('heightAngleEntity')!==undefined)){
                viewer.entities.removeById('heightAngleEntity');
            }
            if((viewer.entities.getById('heighthriEntity')!==undefined)){
                viewer.entities.removeById('heighthriEntity');
            }
            if((viewer.entities.getById('heightLabelAngleEntity')!==undefined)){
                viewer.entities.removeById('heightLabelAngleEntity');
            }
            var isFirst = true;
            var firstPosition;
            var secondPosition;
            var previousPosition;
            var heightLineEntity = viewer.entities.add({
                id : 'measureHeightAngleLineEntity',
                polyline : {
                    followSurface : false,
                    width : 3,
                    material : Cesium.Color.RED,
                    show : true
                }
            });
            var heightAngleEntity = viewer.entities.add({
                id : 'heightAngleEntity',
                polyline : {
                    width : 3,
                    material : Cesium.Color.RED,
                    show : true
                }
            });
            var heighthriEntity = viewer.entities.add({
                id : 'heighthriEntity',
                polyline : {
                    width : 3,
                    material : Cesium.Color.RED,
                    show : true
                }
            });
            var heightLabelEntity = viewer.entities.add({
                id : 'heightLabelAngleEntity',
                label : {
                    show : false,
                    verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                    horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
                    font : '16px Helvetica',
                    fillColor : Cesium.Color.YELLOW,
                    outlineColor : Cesium.Color.BLACK,
                    outlineWidth : 1,
                    style : Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: true,
                    backgroundColor: new Cesium.Color(0, 0, 1, 0.8),
                }
            });
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            handler.setInputAction(function(movement){
                if(isFirst){
                    firstPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(firstPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        firstPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    isFirst = false;
                }else{
                    secondPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(secondPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        secondPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    if(secondPosition){
                        previousPosition = secondPosition.clone();
                    }else{
                        secondPosition = previousPosition.clone();
                    }
                    var centerPosition = new Cesium.Cartesian3(
                        firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition)
                    );
                    heightLineEntity.polyline.positions = [firstPosition,centerPosition];
                    heightAngleEntity.polyline.positions = [firstPosition,secondPosition];
                    heighthriEntity.polyline.positions = [centerPosition,secondPosition];
                    var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition);
                    var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition);
                    heightAngleEntity.polyline.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.RED:Cesium.Color.YELLOW;
                    heighthriEntity.polyline.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.RED:Cesium.Color.YELLOW;
                    heightLabelEntity.position = (cartographicCenter.height>cartographicFirst.height)>0?centerPosition:firstPosition;
                    heightLabelEntity.label.show = true;
                    resultStr = cartographicCenter.height-cartographicFirst.height;
                    var resulthoriStr = Cesium.Cartesian3.distance(centerPosition, secondPosition);
                    var resultangleStr = Cesium.Cartesian3.distance(firstPosition, secondPosition);
                    heightLabelEntity.label.text = '高度:'+resultStr.toFixed(2)+'米\n'+"水平:"+resulthoriStr.toFixed(2)+'米\n'+"直线:"+resultangleStr.toFixed(2)+"米";
                    var camera = viewer.scene.camera;
                    heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0);
                    handler = handler && handler.destroy();
                }
            },Cesium.ScreenSpaceEventType.LEFT_CLICK);
            var resultStr;
            handler.setInputAction(function(movement){
                if(!isFirst){
                    secondPosition = viewer.scene.pickPosition(movement.endPosition);
                    var cartographic = Cesium.Cartographic.fromCartesian(secondPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.endPosition);
                        secondPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    if(secondPosition){
                        previousPosition = secondPosition.clone();
                    }else{
                        secondPosition = previousPosition.clone();
                    }
                    var centerPosition = new Cesium.Cartesian3(
                        firstPosition.x * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.y * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition),
                        firstPosition.z * Cesium.Cartesian3.dot(firstPosition, secondPosition)/Cesium.Cartesian3.magnitudeSquared(firstPosition)
                    );
                    heightLineEntity.polyline.positions = [firstPosition,centerPosition];
                    heightAngleEntity.polyline.positions = [firstPosition,secondPosition];
                    heighthriEntity.polyline.positions = [centerPosition,secondPosition];
                    var cartographicCenter = Cesium.Cartographic.fromCartesian(centerPosition);
                    var cartographicFirst = Cesium.Cartographic.fromCartesian(firstPosition);
                    heightAngleEntity.polyline.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.RED:Cesium.Color.YELLOW;
                    heighthriEntity.polyline.material = (cartographicCenter.height>cartographicFirst.height)>0?Cesium.Color.RED:Cesium.Color.YELLOW;
                    heightLabelEntity.position = (cartographicCenter.height>cartographicFirst.height)>0?centerPosition:firstPosition;
                    heightLabelEntity.label.show = true;
                    resultStr = cartographicCenter.height-cartographicFirst.height;
                    var resulthoriStr = Cesium.Cartesian3.distance(centerPosition, secondPosition);
                    var resultangleStr = Cesium.Cartesian3.distance(firstPosition, secondPosition);
                    heightLabelEntity.label.text = '高度:'+resultStr.toFixed(2)+'米\n'+"水平:"+resulthoriStr.toFixed(2)+'米\n'+"直线:"+resultangleStr.toFixed(2)+"米";
                    var camera = viewer.scene.camera;
                    heightLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0);
                }
            },Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            handler.setInputAction(function(){
                handler = handler && handler.destroy();
            },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return resultStr;
        };
        
        //Get distence String between first left_click and second left_click
        CesiumSurvey.measureDist = function(viewer,type){
            handler = handler && handler.destroy();
            if((viewer.entities.getById('dynamicLineEntity')!==undefined)){
                viewer.entities.removeById('dynamicLineEntity');
            }
            if((viewer.entities.getById('dynamicLineGroundEntity')!==undefined)){
                viewer.entities.removeById('dynamicLineGroundEntity');
            }
            if((viewer.entities.getById('measureLineEntity')!==undefined)){
                viewer.entities.removeById('measureLineEntity');
            }
            if((viewer.entities.getById('measureLineGroundEntity')!==undefined)){
                viewer.entities.removeById('measureLineGroundEntity');
            }
            if((viewer.entities.getById('distLabelEntity')!==undefined)){
                viewer.entities.removeById('distLabelEntity');
            }
            var isFirst = true;//是否为第一个点
            var previousPosition;//前一个点的坐标
            var currentPosition;//当前点坐标
            var measureValue = 0.0;
            //随鼠标移动的标签entity
            var distLabelEntity = viewer.entities.add({
                id : 'distLabelEntity',
                label : {
                    show : false,
                    verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                    horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
                    font : '16px Helvetica',
                    fillColor : Cesium.Color.YELLOW,
                    outlineColor : Cesium.Color.BLACK,
                    outlineWidth : 1,
                    style : Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: true,
                    backgroundColor: new Cesium.Color(0, 0, 1, 0.8),
                }
            });
            var show3d = true,show2d = false;
            if(type=="two"){
                show2d = true;
                show3d = false;
            }
            //前一个点与鼠标连线的entity
            var dynamicLineEntity = viewer.entities.add({
                id : 'dynamicLineEntity',
                polyline : {
                    followSurface : false,
                    width : 3,
                    material : Cesium.Color.YELLOW,
                    show : show3d
                }
            });
            var dynamicLineGroundEntity = viewer.entities.add({
                id : 'dynamicLineGroundEntity',
                polyline : {
                    clampToGround : true,
                    width : 3,
                    material : Cesium.Color.YELLOW,
                    show : show2d,
                }
            });
            //已输入的线段的entity
            var measureLineEntity = viewer.entities.add({
                id : 'measureLineEntity',
                polyline : {
                    followSurface : false,
                    width : 3,
                    material : Cesium.Color.RED,
                    show : show3d
                }
            });
            var measureLineGroundEntity = viewer.entities.add({
                id : 'measureLineGroundEntity',
                polyline : {
                    clampToGround : true,
                    width : 3,
                    material : Cesium.Color.RED,
                    show : show2d
                }
            });
            var measureLinePositonsArray = [];//存储已量测的线段的折点
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            handler.setInputAction(function(movement){
                if(isFirst){
                    previousPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(previousPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        previousPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    var tmp1 = previousPosition.clone();
                    measureValue = 0.0;
                    isFirst = false;
                    measureLinePositonsArray.push(tmp1);
                }else{
                    currentPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(currentPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        currentPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    var tmp2 = currentPosition.clone();
                    measureLinePositonsArray.push(tmp2);
                    measureLineEntity.polyline.positions = measureLinePositonsArray;
                    measureLineGroundEntity.polyline.positions = measureLinePositonsArray;
                    measureValue = measureValue + Cesium.Cartesian3.distance(currentPosition, previousPosition);
                    previousPosition = currentPosition.clone();
                }
            },Cesium.ScreenSpaceEventType.LEFT_CLICK);
            var measureValueShow;
            handler.setInputAction(function(movement){
                //move事件监听
                if(!isFirst){
                    //console.log('move,不是第一个点');
                    //获取当前点的坐标
                    currentPosition = viewer.scene.pickPosition(movement.endPosition);
                    var cartographic = Cesium.Cartographic.fromCartesian(currentPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.endPosition);
                        currentPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    if(currentPosition){
                        distLabelEntity.position = currentPosition;
                        distLabelEntity.label.show = true;
                        measureValueShow = measureValue+Cesium.Cartesian3.distance(currentPosition, previousPosition);
                        distLabelEntity.label.text = '距离:'+ measureValueShow.toFixed(2)+'米';
                        var camera = viewer.scene.camera;
                        distLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 1, 0.0);
                        dynamicLineEntity.polyline.positions = [previousPosition,currentPosition];
                        dynamicLineGroundEntity.polyline.positions = [previousPosition,currentPosition];
                    }
                }
            },Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            handler.setInputAction(function(){
                //move事件监听
                handler = handler && handler.destroy();
            },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return measureValueShow;
        };
        
        //Get area string by dynamic polygon on earth
        var pointArr = [];
        CesiumSurvey.measureArea = function(viewer){
            var measureAreaValue = 0.0;
            var activeShapePoints = [];
            handler = handler && handler.destroy();
            if((viewer.entities.getById('areaLabelEntity')!==undefined)){
                viewer.entities.removeById('areaLabelEntity');
            }
            if((viewer.entities.getById('areaEntity')!==undefined)){
                viewer.entities.removeById('areaEntity');
            }
            for (var i = 0; i < pointArr.length; i++) {
                viewer.entities.remove(pointArr[i]);
            }
            var areaLabelEntity = viewer.entities.add({
                id : 'areaLabelEntity',
                label : {
                    show : false,
                    verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                    horizontalOrigin : Cesium.HorizontalOrigin.LEFT,
                    font : '16px Helvetica',
                    fillColor : Cesium.Color.YELLOW,
                    outlineColor : Cesium.Color.BLACK,
                    outlineWidth : 1,
                    style : Cesium.LabelStyle.FILL_AND_OUTLINE,
                    showBackground: true,
                    backgroundColor: new Cesium.Color(0, 0, 1, 0.8),
                }
            });
            function createPoint(worldPosition) {
                pointArr.push(viewer.entities.add({
                    position : worldPosition,
                    point : {
                        color : Cesium.Color.RED,
                        pixelSize : 10,
                        outlineWidth : 1,
                        outlineColor : Cesium.Color.WHITE,
                        disableDepthTestDistance : 10000
                    }
                }));
            }
            function drawPolygon(){
                if((viewer.entities.getById('areaEntity')!==undefined)){
                    viewer.entities.removeById('areaEntity');
                }
                viewer.entities.add({
                    id: 'areaEntity',
                    polygon: {
                        hierarchy: new Cesium.PolygonHierarchy(activeShapePoints),
                            /*new Cesium.CallbackProperty(function () {
                            return activeShapePoints;
                        }, false),*/
                        material: new Cesium.ColorMaterialProperty(Cesium.Color.YELLOW.withAlpha(0.6)),
                        classificationType : Cesium.ClassificationType.BOTH,
                        outline: true,
                        outlineColor: Cesium.Color.WHITE
                    }
                });
            }
            function distance(point1,point2){
                var point1cartographic = Cesium.Cartographic.fromCartesian(point1);
                var point2cartographic = Cesium.Cartographic.fromCartesian(point2);
                /**根据经纬度计算出距离**/
                var geodesic = new Cesium.EllipsoidGeodesic();
                geodesic.setEndPoints(point1cartographic, point2cartographic);
                var s = geodesic.surfaceDistance;
                //console.log(Math.sqrt(Math.pow(distance, 2) + Math.pow(endheight, 2)));
                //返回两点之间的距离
                s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
                return s;
            }
            var radiansPerDegree = Cesium.Math.PI / 180.0;//角度转化为弧度(rad)
            var degreesPerRadian = 180.0 / Cesium.Math.PI;//弧度转化为角度
            //计算多边形面积
            function getArea(points) {
                var res = 0.0;
                //拆分三角曲面
                for (var i = 0; i < points.length - 2; i++) {
                    var j = (i + 1) % points.length;
                    var k = (i + 2) % points.length;
                    var totalAngle = Angle(points[0], points[j], points[k]);
                    var dis_temp1 = distance(activeShapePoints[0], activeShapePoints[j]);
                    var dis_temp2 = distance(activeShapePoints[0], activeShapePoints[k]);
                    var tmp = 0.5*dis_temp1 * dis_temp2 * Math.sin(totalAngle*radiansPerDegree) ;
                    res = res+tmp;
                    //console.log(res);
                }
                return Math.abs(res.toFixed(1));//顺时针负,逆时针正
            }
            /*角度*/
            function Angle(p1, p2, p3) {
                var bearing12 = Bearing(p1, p2);
                var bearing13 = Bearing(p1, p3);
                var angle = bearing12 - bearing13;
                if (angle < 0) {
                    angle += 360;
                }
                return angle;
            }
            /*方向*/
            function Bearing(from, to) {
                var lat1 = from.lat * radiansPerDegree;
                var lon1 = from.lon * radiansPerDegree;
                var lat2 = to.lat * radiansPerDegree;
                var lon2 = to.lon * radiansPerDegree;
                var angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));
                if (angle < 0) {
                    angle += Cesium.Math.PI * 2.0;
                }
                angle = angle * degreesPerRadian;//角度
                return angle;
            }
            //选点
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            handler.setInputAction(function(movement){
                var earthPosition = viewer.scene.pickPosition(movement.position);
                //获取地理坐标(经度、纬度、高程)
                var cartographic = Cesium.Cartographic.fromCartesian(earthPosition);
                var height = cartographic.height;//模型高度
                if(Number(height)<0){
                    var ray = viewer.camera.getPickRay(movement.position);
                    earthPosition = viewer.scene.globe.pick(ray,viewer.scene);
                }
                if (Cesium.defined(earthPosition)) {
                    activeShapePoints.push(earthPosition);
                    if(activeShapePoints.length>3){
                        drawPolygon();
                    }
                    createPoint(earthPosition);
                }
            },Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //移动
            handler.setInputAction(function(movement) {
                var newPosition = viewer.scene.pickPosition(movement.endPosition);
                var cartographic = Cesium.Cartographic.fromCartesian(newPosition);
                var height = cartographic.height;//模型高度
                if(Number(height)<0){
                    var ray = viewer.camera.getPickRay(movement.endPosition);
                    newPosition = viewer.scene.globe.pick(ray,viewer.scene);
                }
                activeShapePoints.pop();
                activeShapePoints.push(newPosition);
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            //结束并计算
            var areaResult;
            handler.setInputAction(function(movement){
                handler = handler && handler.destroy();
                if(activeShapePoints.length >2){
                    var rightPosition = viewer.scene.pickPosition(movement.position);
                    var cartographic = Cesium.Cartographic.fromCartesian(rightPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.position);
                        rightPosition = viewer.scene.globe.pick(ray,viewer.scene);
                    }
                    var tempPoints=[];
                    for(var pindex = 0;pindex<activeShapePoints.length;pindex++){
                        var cartographic = Cesium.Cartographic.fromCartesian(activeShapePoints[pindex].clone());
                        var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
                        var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
                        var heightString = cartographic.height;
                        tempPoints.push({ lon: longitudeString, lat: latitudeString ,hei:heightString});
                    }
                    drawPolygon();
                    areaResult = getArea(tempPoints);
                    areaLabelEntity.label.text = '面积:'+ areaResult +'平方米';
                    areaLabelEntity.position = rightPosition.clone();
                    areaLabelEntity.label.show = true;
                    var camera = viewer.scene.camera;
                    areaLabelEntity.label.eyeOffset = new Cesium.Cartesian3(0.0, 0.5, 0.0);
                }
            },Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return areaResult;
        };
        
        var measurePointArr = [];
        CesiumSurvey.measurePoint = function(viewer,img){
            var poiResult;
            handler = handler && handler.destroy();
            handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            handler.setInputAction(function(movement){
                //获取鼠标点击处的坐标
                var cartesian = viewer.scene.pickPosition(movement.position);
                var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                var height = cartographic.height;//模型高度
                if(Number(height)<0){
                    var ray = viewer.camera.getPickRay(movement.position);
                    cartesian = viewer.scene.globe.pick(ray,viewer.scene);
                }
                cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                height = cartographic.height;//地形高度
                var lng = Cesium.Math.toDegrees(cartographic.longitude);
                var lat = Cesium.Math.toDegrees(cartographic.latitude);
                var heading = Cesium.Math.toDegrees(viewer.camera.heading);
                var pitch = Cesium.Math.toDegrees(viewer.camera.pitch);
                var roll = Cesium.Math.toDegrees(viewer.camera.roll);
                poiResult = lng.toFixed(4)+"\u00B0"+"\n"+lat.toFixed(4)+"\u00B0"+"\n"+height.toFixed(1)+"米";
                console.log(lng+','+lat+','+height+','+heading+','+pitch+','+roll);
                var pinBuilder = new Cesium.PinBuilder();//创建cesium pin标签
                var point;
                if(img&&img!=''){
                    point = viewer.entities.add({
                        id : 'measurePointEntity'+new Date().getTime(),
                        position : cartesian.clone(),
                        billboard : {
                            image : img,
                            verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                            pixelOffset : new Cesium.Cartesian2(43.0, 0.0),
                        },
                        label : {
                            show : true,
                            verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                            horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
                            font : 'bold 16px Helvetica',
                            fillColor : Cesium.Color.WHITE,
                            text : poiResult,
                            pixelOffset : new Cesium.Cartesian2(56.0, -126.0),
                            disableDepthTestDistance : 100000000,
                        }
                    });
                }else{
                    point = viewer.entities.add({
                        id : 'measurePointEntity'+new Date().getTime(),
                        position : cartesian.clone(),
                        billboard : {
                            image : pinBuilder.fromText('测',Cesium.Color.RED,36).toDataURL(),
                            verticalOrigin : Cesium.VerticalOrigin.BOTTOM
                        },
                        label : {
                            show : true,
                            verticalOrigin : Cesium.VerticalOrigin.BOTTOM,
                            horizontalOrigin : Cesium.HorizontalOrigin.CENTER,
                            font : '16px sans-serif',
                            fillColor : Cesium.Color.YELLOW,
                            outlineColor : Cesium.Color.BLACK,
                            outlineWidth : 1,
                            style : Cesium.LabelStyle.FILL_AND_OUTLINE,
                            text : poiResult,
                            pixelOffset : new Cesium.Cartesian2(0.0, -14.0),
                            pixelOffsetScaleByDistance : new Cesium.NearFarScalar(1.5e2, 3.0, 1.5e7, 0.5),
                            backgroundColor: Cesium.Color.BLUE.withAlpha(0.8),
                            showBackground: true,
                        }
                    });
                }
                measurePointArr.push(point);
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            handler.setInputAction(function(movement){
                handler = handler && handler.destroy();
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            return poiResult;
        };
        
        var mmhandler;
        CesiumSurvey.measureMovePoint = function(viewer,resultid){
            mmhandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            //移动
            mmhandler.setInputAction(function(movement) {
                var newPosition = viewer.scene.pickPosition(movement.endPosition);
                if(newPosition!=undefined){
                    var cartographic = Cesium.Cartographic.fromCartesian(newPosition);
                    var height = cartographic.height;//模型高度
                    if(Number(height)<0){
                        var ray = viewer.camera.getPickRay(movement.endPosition);
                        newPosition = viewer.scene.globe.pick(ray,viewer.scene);
                        cartographic = Cesium.Cartographic.fromCartesian(newPosition);
                    }
                    height = cartographic.height;//地形高度
                    var lng = Cesium.Math.toDegrees(cartographic.longitude);
                    var lat = Cesium.Math.toDegrees(cartographic.latitude);
                    var camPoiR = viewer.camera.position;
                    var camcartographic = Cesium.Cartographic.fromCartesian(camPoiR);
                    var camheight = camcartographic.height;//地形高度
                    var camlng = Cesium.Math.toDegrees(camcartographic.longitude);
                    var camlat = Cesium.Math.toDegrees(camcartographic.latitude);
                    var heading = Cesium.Math.toDegrees(viewer.camera.heading);
                    var pitch = Cesium.Math.toDegrees(viewer.camera.pitch);
                    var roll = Cesium.Math.toDegrees(viewer.camera.roll);
                    var poiResult = lng.toFixed(4)+"\u00B0"+","+lat.toFixed(4)+"\u00B0"+","+height.toFixed(1)+"米"
                    document.getElementById(resultid).innerHTML = poiResult;
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        };
        
        var cammmhandler;
        CesiumSurvey.cammeasureMovePoint = function(viewer,camresultid){
            cammmhandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            //移动
            cammmhandler.setInputAction(function(movement) {
                var camPoiR = viewer.camera.position;
                var camcartographic = Cesium.Cartographic.fromCartesian(camPoiR);
                var camheight = camcartographic.height;//地形高度
                var camlng = Cesium.Math.toDegrees(camcartographic.longitude);
                var camlat = Cesium.Math.toDegrees(camcartographic.latitude);
                var heading = Cesium.Math.toDegrees(viewer.camera.heading);
                var pitch = Cesium.Math.toDegrees(viewer.camera.pitch);
                var roll = Cesium.Math.toDegrees(viewer.camera.roll);
                var camResult = camlng.toFixed(4)+"\u00B0"+","+camlat.toFixed(4)+"\u00B0"+","+camheight.toFixed(1)+"米,"
                                +heading.toFixed(2)+"\u00B0"+","+pitch.toFixed(2)+"\u00B0"+","+roll.toFixed(1)+"\u00B0";
                document.getElementById(camresultid).innerHTML = camResult;
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        };
        
        //Clear measure result
        CesiumSurvey.measureClear = function(viewer){
            handler = handler && handler.destroy();
            if((viewer.entities.getById('measureHeightLineEntity')!==undefined)){
                viewer.entities.removeById('measureHeightLineEntity');
            }
            if((viewer.entities.getById('heightPlaneEntity')!==undefined)){
                viewer.entities.removeById('heightPlaneEntity');
            }
            if((viewer.entities.getById('heightLabelEntity')!==undefined)){
                viewer.entities.removeById('heightLabelEntity');
            }
            if((viewer.entities.getById('measureHeightAngleLineEntity')!==undefined)){
                viewer.entities.removeById('measureHeightAngleLineEntity');
            }
            if((viewer.entities.getById('heightAngleEntity')!==undefined)){
                viewer.entities.removeById('heightAngleEntity');
            }
            if((viewer.entities.getById('heighthriEntity')!==undefined)){
                viewer.entities.removeById('heighthriEntity');
            }
            if((viewer.entities.getById('heightLabelAngleEntity')!==undefined)){
                viewer.entities.removeById('heightLabelAngleEntity');
            }
            if((viewer.entities.getById('dynamicLineEntity')!==undefined)){
                viewer.entities.removeById('dynamicLineEntity');
            }
            if((viewer.entities.getById('dynamicLineGroundEntity')!==undefined)){
                viewer.entities.removeById('dynamicLineGroundEntity');
            }
            if((viewer.entities.getById('measureLineEntity')!==undefined)){
                viewer.entities.removeById('measureLineEntity');
            }
            if((viewer.entities.getById('measureLineGroundEntity')!==undefined)){
                viewer.entities.removeById('measureLineGroundEntity');
            }
            if((viewer.entities.getById('distLabelEntity')!==undefined)){
                viewer.entities.removeById('distLabelEntity');
            }
            if((viewer.entities.getById('areaLabelEntity')!==undefined)){
                viewer.entities.removeById('areaLabelEntity');
            }
            if((viewer.entities.getById('areaEntity')!==undefined)){
                viewer.entities.removeById('areaEntity');
            }
            for (var i = 0; i < pointArr.length; i++) {
                viewer.entities.remove(pointArr[i]);
            }
            if((viewer.entities.getById('measurePointEntity')!==undefined)){
                viewer.entities.removeById('measurePointEntity');
            }
            for ( var i = 0; i < measurePointArr.length; i++) {
                viewer.entities.remove(measurePointArr[i]);
            }
            //measurePointArr = [];
            return true;
        };
        
        //shutdown handler
        CesiumSurvey.clearHandler = function(){
            handler = handler && handler.destroy();
        };
        
        //shutdown mmhandler
        CesiumSurvey.clearMMHandler = function(){
            mmhandler = mmhandler && mmhandler.destroy();
        };
        
        //shutdown cammmhandler
        CesiumSurvey.clearCamMMHandler = function(){
            cammmhandler = cammmhandler && cammmhandler.destroy();
        };
        
        return CesiumSurvey;
    }
    if (typeof(CesiumSurvey) === 'undefined') {
        window.CesiumSurvey = define_CesiumSurvey();
    } else {
        console.log("CesiumSurvey already defined.");
    }
})(window);