-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
1275 lines (1140 loc) · 39.5 KB
/
map.js
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// var local_ip = "192.168.0.107"; //局域网上的本机IP地址,若发布至互联网后,填写公网ip
// var local_ip = "localhost";
var local_ip = "www.skylight.xin";
function getQueryVariable(variable) {
const query = window.location.search.substring(1);
const vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return '';
}
function QueryToJSON() {
const query = window.location.search.substring(1);
var vars = query.split("&");
var json = {};
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
json[pair[0]] = pair[1];
}
return json;
}
var MapControl = function (opts) {
var me = this;
me.opts = $.extend(true, {//opts中的配置会覆盖以下默认配置
cesium_container: "cesium_container",
toolbar_container: "toolbar_1",
view_center: [105, 35, 15000000],
home_orientation: {
heading: 0,
pitch: -90,
roll: 0
},
point_orientation: {
heading: 0,
pitch: -45,
roll: 0
},
altitude_range: {
min: 0.0,
max: 8848.0
},
model: "",
is_global: true,
show_atmosphere: true,
enable_light: false,
cesium_access_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzNTYyZmIwOC00MDVhLTRlODEtYTRmYS1lZGU4YjhmMWJlZDIiLCJpZCI6NDUxNTksImlhdCI6MTYyNjkzNTA4NH0.WG1cA1YXgEQFE4Dw7aGdailrJaxSRaRuLsRsfoyhbik"
}, opts);
me.pathpoints = Array();
me.home_orientation = {
// 指向
heading: Cesium.Math.toRadians(me.opts.home_orientation.heading),
// 视角
pitch: Cesium.Math.toRadians(me.opts.home_orientation.pitch),
roll: Cesium.Math.toRadians(me.opts.home_orientation.roll)
}
me.point_orientation = {
// 指向
heading: Cesium.Math.toRadians(me.opts.point_orientation.heading),
// 视角
pitch: Cesium.Math.toRadians(me.opts.point_orientation.pitch),
roll: Cesium.Math.toRadians(me.opts.point_orientation.roll)
}
me.read_query();
me.init();
me.contour_init();
me.search_init();
};
MapControl.prototype.read_query = function () {
var me = this;
var query = QueryToJSON();
if (query["data"]) {
me.path_polyline = JSON.parse(atob(query["data"]));
}
}
MapControl.prototype.set_query = function () {
var me = this;
}
MapControl.prototype.init = function () {
var me = this;
var tianditu_token = 'c23af70822a130e1822f8464dd6e9fd6'
var mapbox_token = 'pk.eyJ1IjoiZ3dpbmc5NyIsImEiOiJja251ZG5uNmswYTZlMnhvend0ejRpZHExIn0.krjMt87hQNfv3NGzyYvhng'
// 服务域名
var tdtUrl = 'https://t{s}.tianditu.gov.cn/'
// 服务负载子域
var img_tianditu_rs = new Cesium.ProviderViewModel({
name: "天地图",
tooltip: "天地图",
iconUrl: "images/tianditu.png",
creationFunction: function () {
var subdomains = ['0', '1', '2', '3', '4', '5', '6', '7']
var providers = Array()
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=img_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 18
}))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains,
tilingScheme: new Cesium.WebMercatorTilingScheme()
}))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 18
}))
return providers;
}
});
var img_google_rs = new Cesium.ProviderViewModel({
name: "Google影像底图",
tooltip: "Google影像底图",
iconUrl: "images/GoogleEarth.ico",
creationFunction: function () {
var provider = new Cesium.UrlTemplateImageryProvider({
url: 'https://www.google.com/maps/vt?lyrs=s&x={x}&y={y}&z={z}'
// subdomains: subdomains
});
return provider;
}
});
var img_google_tianditu_rs = new Cesium.ProviderViewModel({
name: "Google影像底图-带天地图注记",
tooltip: "Google影像底图-带天地图注记",
iconUrl: "images/GoogleEarth.ico",
creationFunction: function () {
var subdomains_tianditu = ['0', '1', '2', '3', '4', '5', '6', '7']
var providers = []
providers.push(new Cesium.UrlTemplateImageryProvider({
url: 'https://www.google.com/maps/vt?lyrs=s&x={x}&y={y}&z={z}'
}))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains_tianditu,
tilingScheme: new Cesium.WebMercatorTilingScheme()
}))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains_tianditu,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 18
}))
return providers;
}
});
var img_mapbox_rs = new Cesium.ProviderViewModel({
name: "MapBox影像底图",
tooltip: "MapBox影像底图",
iconUrl: "images/mapbox.png",
creationFunction: function () {
var provider = new Cesium.UrlTemplateImageryProvider({
url: 'https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/{z}/{x}/{y}?access_token=' + mapbox_token
});
return provider;
}
});
var img_mapbox_tianditu_rs = new Cesium.ProviderViewModel({
name: "MapBox影像底图-带天地图注记",
tooltip: "MapBox影像底图-带天地图注记",
iconUrl: "images/mapbox.png",
creationFunction: function () {
var subdomains_tianditu = ['0', '1', '2', '3', '4', '5', '6', '7']
var providers = []
providers.push(new Cesium.UrlTemplateImageryProvider({
url: 'https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/{z}/{x}/{y}?access_token=' + mapbox_token
}))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains_tianditu,
tilingScheme: new Cesium.WebMercatorTilingScheme()
}))
// providers.push(new Cesium.UrlTemplateImageryProvider({
// url: "https://wprd04.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=2&style=8<ype=11",
// subdomains: subdomains_tianditu,
// tilingScheme : new Cesium.WebMercatorTilingScheme()
// }))
providers.push(new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + 'DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=' + tianditu_token,
subdomains: subdomains_tianditu,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 18
}))
return providers;
}
});
var img_google_topo = new Cesium.ProviderViewModel({
name: "Google地形图",
tooltip: "Google地形图",
iconUrl: "images/google_map.jpg",
creationFunction: function () {
var provider = new Cesium.UrlTemplateImageryProvider({
url: 'https://www.google.com/maps/vt?lyrs=p&x={x}&y={y}&z={z}'
});
return provider;
}
});
var img_osm_topo = new Cesium.ProviderViewModel({
name: "OpenStreetMap地形图",
tooltip: "OpenStreetMap地形图",
iconUrl: "images/osm_logo.png",
creationFunction: function () {
var provider = new Cesium.UrlTemplateImageryProvider({
url: "https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=6170aad10dfd42a38d4d8c709a536f38"
// url: "https://b.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png"
// url: "https://b.tile.opentopomap.org/{z}/{x}/{y}.png"
})
return provider;
}
});
//var img_arcgis = new Cesium.ProviderViewModel({
// name: "ArcGIS地图", //作为三维地图显示效果并不好
// tooltip: "ArcGIS地图",
// creationFunction: function () {
// var provider = new Cesium.UrlTemplateImageryProvider({
// url: "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}"
// })
// return provider;
// }
//});
var img_WorldSoil = new Cesium.ProviderViewModel({
name: "全球土壤类型 2006",
tooltip: "全球土壤类型 2006",
iconUrl: "./images/soil.png",
creationFunction: function () {
var provider = new Cesium.WebMapServiceImageryProvider({
url: "https://maps.isric.org/mapserv?map=/map/wrb.map",
layers: "MostProbable",
tileHeight: 256,
tileWidth: 256,
parameter: {
service: 'WMS',
version: '1.3.0',
request: 'GetMap',
style: 'default',
transparent: 'true',
format: 'image/png',
CRS: 'EPSG:4326'
}
})
return provider;
}
});
var cesium_terrain = new Cesium.ProviderViewModel({
name: "Cesium全球地形",
tooltip: "Cesium全球地形",
iconUrl: "./images/CesiumWorldTerrain.png",
creationFunction: function () {
return Cesium.createWorldTerrain({
// required for water effects
requestWaterMask: true,
// required for terrain lighting
requestVertexNormals: true
});
}
});
var ellipsoid_terrain = new Cesium.ProviderViewModel({
name: "大地椭球体",
tooltip: "大地椭球体",
iconUrl: "./images/Ellipsoid.png",
creationFunction: function () {
var provider = new Cesium.EllipsoidTerrainProvider();
return provider;
}
});
Cesium.Ion.defaultAccessToken = me.opts.cesium_access_token;
let enable_animation = false;
let enable_timeline = false;
if(me.opts.enable_time_widgt){
enable_animation = true;
enable_timeline = true;
}
me.viewer = new Cesium.Viewer(me.opts.cesium_container, {
animation: enable_animation, //是否创建动画小器件,左下角仪表
timeline: enable_timeline, //是否显示时间线控件
geocoder: false, //是否显示地名查找控件,右上角查询按钮
fullscreenButton: false, //是否显示右下角全屏按钮
navigationHelpButton: false, //是否显示右上角的帮助按钮
homeButton: true, //是否显示Home按钮
showRenderLoopErrors: false,//如果设为true,将在一个HTML面板中显示错误信息
sceneModePicker: true,//是否显示3D/2D选择器
baseLayerPicker: true,//是否显示图层选择器
scene3DOnly: false,//如果设置为true,则所有几何图形以3D模式绘制以节约GPU资源
infoBox: false,//是否显示信息框
selectionIndicator: false,
//vrButton: true, //VR模式
imageryProviderViewModels: [
img_google_rs,
img_google_tianditu_rs,
img_google_topo,
img_mapbox_rs,
img_mapbox_tianditu_rs,
img_tianditu_rs,
img_osm_topo,
img_WorldSoil
],//可供BaseLayerPicker选择的图像图层ProviderViewModel数组
terrainProviderViewModels: [cesium_terrain, ellipsoid_terrain],
// 解决截图后图片没有内容,无法得到地图场景的问题
contextOptions: {
webgl: {
alpha: true,
depth: true,
stencil: true,
antialias: true,
premultipliedAlpha: true, //通过canvas.toDataURL()实现截图需要将该项设置为true
preserveDrawingBuffer: true,
failIfMajorPerformanceCaveat: true
}
},
});
if (!me.opts.show_atmosphere) {
me.viewer.scene.skyBox.show = false; //天空盒,即星空贴图
me.viewer.scene.skyAtmosphere.show = false; //大气效果
// me.viewer.scene.globe.showGroundAtmosphere = true;
//隐藏地球默认的蓝色背景
me.viewer.scene.globe.baseColor = Cesium.Color.TRANSPARENT;
//隐藏雾效果
me.viewer.scene.fog.enabled = false;
//隐藏黑色背景
//me.viewer.scene.backgroundColor=Cesium.Color.TRANSPARENT;
}
me.viewer._cesiumWidget._creditContainer.style.display = "none"; //去除版权信息
// 判断是否支持图像渲染像素化处理
var supportsImageRenderingPixelated = me.viewer.cesiumWidget._supportsImageRenderingPixelated;
if (supportsImageRenderingPixelated) {
// 直接拿到设备的像素比例因子 - 如我设置的1.25
var vtxf_dpr = window.devicePixelRatio;
while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
// 设置渲染分辨率的比例因子
me.viewer.resolutionScale = vtxf_dpr;
}
me.viewer.scene.fxaa = true
me.viewer.scene.postProcessStages.fxaa.enabled = true
//设置MapBox为默认的遥感影像
me.viewer.baseLayerPicker.viewModel.selectedImagery = me.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[4];
me.viewer.baseLayerPicker.viewModel.selectedTerrain = me.viewer.baseLayerPicker.viewModel.terrainProviderViewModels[0];
var tianditu_flag = false
var google_flag = false
//若可以连接上Google,则设置Google影像为默认的遥感影像
$.ajax({
url: "https://www.google.com/maps/vt?lyrs=s&x=0&y=0&z=0",
method: "get",
success: function () {
google_flag = true
console.log("成功连接至Google服务器");
//若可以获取天地图标注图层,则添加带有天地图标注图层的MapBox影像
$.ajax({
url: 'https://t0.tianditu.gov.cn/DataServer?T=ibo_w&x=0&y=0&l=0&tk=' + tianditu_token,
method: "get",
success: function () {
tianditu_flag = true;
console.log("成功获取天地图标注图层");
if(tianditu_flag){
if(google_flag){
me.viewer.baseLayerPicker.viewModel.selectedImagery = me.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[1];
} else {
me.viewer.baseLayerPicker.viewModel.selectedImagery = me.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[4];
}
} else {
if(google_flag){
me.viewer.baseLayerPicker.viewModel.selectedImagery = me.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[0];
} else {
me.viewer.baseLayerPicker.viewModel.selectedImagery = me.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[3];
}
}
},
error: function () {
tianditu_flag = false
console.log("无法获取天地图标注图层");
},
timeout: 5000
})
},
error: function () {
google_flag = false
console.log("无法连接至Google服务器");
},
timeout: 5000
})
if(!me.opts.is_global){
let SenceRectangle = Cesium.Rectangle.fromDegrees(
me.opts.view_center[0] - 0.4,
me.opts.view_center[1] - 0.3,
me.opts.view_center[0] + 0.4,
me.opts.view_center[1] + 0.3
);
me.viewer.scene.globe.cartographicLimitRectangle = SenceRectangle;
me.viewer.scene.globe.backFaceCulling = true;
}
if(me.opts.enable_light){
//开启光照
me.viewer.scene.globe.enableLighting = true;
me.viewer.scene.globe.lightingFadeOutDistance = 1e6;
me.viewer.scene.globe.lightingFadeInDistance = 2e6;
me.viewer.scene.globe.nightFadeOutDistance = 1e6;
me.viewer.scene.globe.nightFadeInDistance = 2e6;
//设置时间,调整太阳高度角
me.viewer.clockViewModel.currentTime = Cesium.JulianDate.fromDate(new Date());
}
//调试用选项
//me.viewer.extend(Cesium.viewerCesiumInspectorMixin);
//显示帧速
//me.viewer.scene.debugShowFramesPerSecond = true;
//添加OSM Building三维建筑数据
//me.viewer.scene.primitives.add(Cesium.createOsmBuildings());
// Enable depth testing so things behind the terrain disappear.
me.viewer.scene.globe.depthTestAgainstTerrain = true;
if (!me.opts.show_atmosphere) {
me.viewer.scene.skyBox.show = false; //天空盒,即星空贴图
me.viewer.scene.skyAtmosphere.show = false; //大气效果
me.viewer.scene.globe.showGroundAtmosphere = true;
//隐藏地球默认的蓝色背景
me.viewer.scene.globe.baseColor = Cesium.Color.TRANSPARENT;
//隐藏雾效果
me.viewer.scene.fog.enabled = false;
//隐藏黑色背景
//me.viewer.scene.backgroundColor=Cesium.Color.TRANSPARENT;
}
//导航栏
var options = {
defaultResetView: Cesium.Cartographic.fromDegrees(...me.opts.view_center),
enableCompass: true,
enableZoomControls: true,
enableDistanceLegend: true,
enableCompassOuterRing: false
}
// extend our view by the cesium navigaton mixin
me.viewer.extend(Cesium.viewerCesiumNavigationMixin, options);
if(me.opts.enable_time_widgt){
$(".coordination").css({
"bottom": "35px",
"right": "10px",
"left": "auto"
});
$(".distance-legend").css({
"bottom": "70px"
});
}
var orientation_setting
if(me.opts.is_global){
orientation_setting = me.home_orientation
} else {
orientation_setting = me.point_orientation
}
// 修改home按钮的行为
me.viewer.homeButton.viewModel.command.beforeExecute.addEventListener(function (commandInfo) {
//飞回指定位置
me.viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
location.longitude - location.height_above * Math.cos(orientation_setting.pitch) * Math.sin(orientation_setting.heading) / 111319.55,
location.latitude - location.height_above * Math.cos(orientation_setting.pitch) * Math.cos(orientation_setting.heading) / 111319.55,
location.height_above * Math.sin(-orientation_setting.pitch)
),
orientation: orientation_setting
});
// Tell the home button not to do anything
commandInfo.cancel = true;
});
let location = {
longitude: me.opts.view_center[0],
latitude: me.opts.view_center[1],
height_above: me.opts.view_center[2]
}
me.viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(
location.longitude - location.height_above * Math.cos(orientation_setting.pitch) * Math.sin(orientation_setting.heading) / 111319.55,
location.latitude - location.height_above * Math.cos(orientation_setting.pitch) * Math.cos(orientation_setting.heading) / 111319.55,
location.height_above * Math.sin(-orientation_setting.pitch)
),
orientation: orientation_setting
});
var longitude_show = $('#longitude_show').get(0);
var latitude_show = $('#latitude_show').get(0);
var altitude_show = $('#altitude_show').get(0);
var view_height = $('#view_height_show').get(0);
var canvas = me.viewer.scene.canvas;
function show_coordinate(movement, type) {
//捕获椭球体,将笛卡尔二维平面坐标转为椭球体的笛卡尔三维坐标,返回球体表面的点
if (movement.endPosition) {
var cartesian = me.viewer.scene.pickPosition(movement.endPosition);
} else if (movement.position) {
var cartesian = me.viewer.scene.pickPosition(movement.position);
}
if (cartesian) {
//将笛卡尔三维坐标转为地图坐标(弧度)
var cartographic = me.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
//将地图坐标(弧度)转为十进制的度数
var log_String = Cesium.Math.toDegrees(cartographic.longitude).toFixed(6);
var lat_String = Cesium.Math.toDegrees(cartographic.latitude).toFixed(6);
var alti_String = (me.viewer.scene.globe.getHeight(cartographic)).toFixed(2);
var view_String = (me.viewer.camera.positionCartographic.height / 1000).toFixed(2);
longitude_show.innerHTML = log_String;
latitude_show.innerHTML = lat_String;
altitude_show.innerHTML = alti_String;
view_height.innerHTML = view_String;
}
}
//具体事件的实现
var handler = new Cesium.ScreenSpaceEventHandler(canvas);
handler.setInputAction(show_coordinate, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handler.setInputAction(show_coordinate, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
MapControl.prototype.get_altitude = function (longitude, latitude) {
var me = this;
return me.viewer.scene.globe.getHeight(
Cesium.Cartographic.fromDegrees(longitude, latitude)
);
}
MapControl.prototype.id = 0;
MapControl.prototype.get_id = function () {
this.id += 1;
return this.id;
}
MapControl.prototype.select_pathpoint = function () {
var me = this;
// 选择entity的监听器,代码自行调整,可以在overlay的基础上继承写成bubble类
var handler = new Cesium.ScreenSpaceEventHandler(me.viewer.scene._imageryLayerCollection);
handler.setInputAction(function (movement) {
var overlay = new Overlay({
MapControl: me,
show_address: true
});
me.viewer.addOverlay(overlay);
var cartesian = me.viewer.scene.pickPosition(movement.position);
if (typeof cartesian != "undefined") {
let ray = me.viewer.camera.getPickRay(movement.position);
cartesian = me.viewer.scene.globe.pick(ray, me.viewer.scene);
overlay.setPosition(cartesian);
}
handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
MapControl.prototype.add_pathpoint = function (location) {
var me = this;
me.pathpoints.push(location);
// console.log(me.pathpoints);
}
MapControl.prototype.remove_pathpoint = function (id) {
var me = this;
let i = me.pathpoints.findIndex((point) => point.id == id);
if (i != -1) {
me.pathpoints.splice(i, 1);
}
// console.log(me.pathpoints);
}
MapControl.prototype.flyTo = function (location, callback) {
var me = this;
var viewer = me.viewer
let approx_height = 12000;
if (location.altitude) {
approx_height = location.altitude + 5 * location.accuracy;
}
if (callback) {
callback(location);
}
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
location.longitude,
location.latitude,
approx_height
),
orientation: me.home_orientation,
complete: () => {
var helper = new Cesium.EventHelper();
var zoomIn = 10;
helper.add(viewer.scene.globe.tileLoadProgressEvent, function (event) {
//console.log("每次加载矢量切片都会进入这个回调")
if (event < 10) {
viewer.camera.zoomIn(zoomIn);
zoomIn += 5;
}
if (event == 0) {
//console.log("这个是加载最后一个矢量切片的回调")
let approx_height = 0;
if (location.altitude) {
approx_height = location.altitude;
} else {
approx_height = me.get_altitude(location.longitude, location.latitude);
}
let height_above = 4000;
if (location.accuracy) {
height_above = 5 * location.accuracy;
}
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
location.longitude - height_above * Math.cos(me.point_orientation.pitch) * Math.sin(me.point_orientation.heading) / 111319.55,
location.latitude - height_above * Math.cos(me.point_orientation.pitch) * Math.cos(me.point_orientation.heading) / 111319.55,
approx_height + height_above * Math.sin(-me.point_orientation.pitch)
),
orientation: me.point_orientation
});
if (callback) {
callback(location);
}
helper.removeAll();
}
});
}
});
}
MapControl.prototype.draw_point = function (location, text = null, id = 'location') {
var me = this;
var viewer = me.viewer;
viewer.entities.remove(viewer.entities.getById(id))
var label = null;
if (text) {
label = {
text: text,
font: '18px sans-serif',
fillColor: Cesium.Color.GOLD,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
outlineWidth: 2,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: new Cesium.Cartesian2(20, -20),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
}
viewer.entities.add({
id: id,
position: Cesium.Cartesian3.fromDegrees(
location.longitude,
location.latitude,
me.get_altitude(location.longitude, location.latitude)
),
point: {
pixelSize: 10,
color: Cesium.Color.BLUE,
outlineColor: Cesium.Color.WHITE,
outlineWidth: 1,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
label: label
})
}
MapControl.prototype.draw_polyline = function (locations) {
var me = this;
var positions = []
locations.forEach((loc, i) => {
positions.push(Cesium.Cartesian3.fromDegrees(
loc.longitude,
loc.latitude,
me.get_altitude(loc.longitude, loc.latitude)
));
});
var line_options = {
name: 'route',
polyline: {
show: true,
positions: positions,
material: Cesium.Color.CHARTREUSE,
width: 5,
clampToGround: true
}
};
me.viewer.entities.add(line_options);
// var helper = new Cesium.EventHelper();
// helper.add(viewer.scene.globe.tileLoadProgressEvent, function (event) {
// //console.log("每次加载矢量切片都会进入这个回调")
// if (event == 0) {
// //console.log("这个是加载最后一个矢量切片的回调")
// line_options.polyline.positions = positions;
// helper.removeAll();
// }
// });
}
//定位
MapControl.prototype.getLocation = function () {
var me = this;
var viewer = me.viewer;
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
me.flyTo(crd, (location) => me.draw_point(location, "当前位置"))
// console.log('Your current position is:')
// console.log('Latitude : ' + crd.latitude)
// console.log('Longitude: ' + crd.longitude)
// console.log('altitude: ' + crd.altitude);
// console.log('More or less ' + crd.accuracy + ' meters.')
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
switch (error.code) {
case error.PERMISSION_DENIED:
alert("用户拒绝告知地理位置")
break;
case error.POSITION_UNAVAILABLE:
alert("位置信息不可用")
break;
case error.TIMEOUT:
alert("请求用户地理位置超时")
break;
case error.UNKNOWN_ERROR:
alert("未知错误")
break;
}
};
if (window.h5sdk) {
alert("调用定位API")
window.h5sdk.device.geolocation.get({
useCache: false,
onSuccess: function (locationData) {
alert(JSON.stringify(locationData));
success(locationData)
},
onFail: function (errorMsg) {
alert(JSON.stringify(errorMsg));
}
});
} else {
navigator.geolocation.getCurrentPosition(success, error, options);
}
}
MapControl.prototype.search_init = function () {
var me = this;
$("#form_search").bind("submit", () => {
me.search();
return false; //防止form自动跳转
})
$('#route_planning').click(() => {
me.route_planning();
});
}
MapControl.prototype.search = function () {
var me = this;
var amap = new amap_api();
amap.search($("input#search").val(), (result) => {
if (result.status == "1") {
toggleToolbarById(me.opts.toolbar_container);
let place_name = result.pois[0].name;
let amap_crds = result.pois[0].location.split(',');
let crd_trans = new Coordinate();
let wgs84_crds = crd_trans.gcj02_to_wgs84(amap_crds[0], amap_crds[1]);
me.flyTo({
longitude: wgs84_crds[0],
latitude: wgs84_crds[1]
}, (location) => me.draw_point(location, place_name, "search_place"))
}
});
}
MapControl.prototype.share = function () {
if (window.h5sdk) {
alert("调用分享API")
window.h5sdk.biz.util.share({
url: "http://www.skylight.xin?data=" + btoa(JSON.stringify(me.pathpoints)),
title: "看看地球",
image: "http://www.skylight.xin/images/mountain.png",
content: "可以浏览全球三维地形图,并在飞书文档里分享地点和路线",
onSuccess: function (result) {
alert(JSON.stringify(result));
}
});
}
}
MapControl.prototype.route_planning = function () {
var me = this;
var amap = new amap_api();
var crd_trans = new Coordinate();
me.viewer.entities.removeAll();
me.draw_point(me.pathpoints[0], null, me.pathpoints[0].id);
for (let i = 0; i < me.pathpoints.length - 1; i++) {
let start = crd_trans.wgs84_to_gcj02(
me.pathpoints[i].longitude,
me.pathpoints[i].latitude
);
let end = crd_trans.wgs84_to_gcj02(
me.pathpoints[i + 1].longitude,
me.pathpoints[i + 1].latitude
);
let start_string = start[0].toFixed(6) + ',' + start[1].toFixed(6);
let end_string = end[0].toFixed(6) + ',' + end[1].toFixed(6);
amap.route_planning(start_string, end_string, (result) => {
// console.log(result);
if (result.status == 1) {
var locations = [];
result.route.paths.forEach((path, i) => {
path.steps.forEach((step, i) => {
step.polyline.split(';').forEach((point, i) => {
let wgs84_crds = crd_trans.gcj02_to_wgs84(
parseFloat(point.split(',')[0]),
parseFloat(point.split(',')[1])
);
locations.push({
longitude: wgs84_crds[0],
latitude: wgs84_crds[1]
})
})
});
});
me.draw_polyline(locations);
} else {
alert("路线规划失败")
}
});
me.draw_point(me.pathpoints[i + 1], null, me.pathpoints[i + 1].id);
}
}
//截图
function save_to_file(scene) {
let canvas = scene.canvas;
let image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
let link = document.createElement("a");
let blob = data_url_to_blob(image);
let objurl = URL.createObjectURL(blob);
link.download = "scene.png";
link.href = objurl;
link.click();
}
function data_url_to_blob(dataurl) {
let arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
//测量空间直线距离
/********************************************/
MapControl.prototype.measureLineSpace = function () {
var me = this;
var viewer = me.viewer;
// 取消双击事件-追踪该位置
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
handler = new Cesium.ScreenSpaceEventHandler(viewer.scene._imageryLayerCollection);
var positions = [];
var poly = null;
// var tooltip = document.getElementById("toolTip");
var distance = 0;
var cartesian = null;
var floatingPoint;
// tooltip.style.display = "block";
handler.setInputAction(function (movement) {
// tooltip.style.left = movement.endPosition.x + 3 + "px";
// tooltip.style.top = movement.endPosition.y - 25 + "px";
// tooltip.innerHTML = '<p>单击开始,右击结束</p>';
cartesian = viewer.scene.pickPosition(movement.endPosition);
if (typeof cartesian != "undefined") {
let ray = viewer.camera.getPickRay(movement.endPosition);
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
//cartesian = viewer.scene.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
if (positions.length >= 2) {
if (!Cesium.defined(poly)) {
poly = new PolyLinePrimitive(positions);
} else {
positions.pop();
// cartesian.y += (1 + Math.random());
positions.push(cartesian);
}
distance = getSpaceDistance(positions);
// console.log("distance: " + distance);
// tooltip.innerHTML='<p>'+distance+'米</p>';
}
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handler.setInputAction(function (movement) {
// tooltip.style.display = "none";
// cartesian = viewer.scene.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);
cartesian = viewer.scene.pickPosition(movement.position);
if (typeof cartesian != "undefined") {
let ray = viewer.camera.getPickRay(movement.position);
cartesian = viewer.scene.globe.pick(ray, viewer.scene);
if (positions.length == 0) {
positions.push(cartesian.clone());
}
positions.push(cartesian);
//在三维场景中添加Label
// var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var textDisance = distance + "米";
// console.log(textDisance + ",lng:" + cartographic.longitude/Math.PI*180.0);
floatingPoint = viewer.entities.add({
name: '空间直线距离',
// position: Cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180,cartographic.height),
position: positions[positions.length - 1],