# cesium-操作面要素透明度
# 面要素GeoJSON
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"stroke": "#555555",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#555555",
"fill-opacity": 0.5
},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.4965763092041, 29.83729356534345], [87.49288558959961, 29.832602908274293], [87.49176979064941, 29.82977351657785], [87.4921131134033, 29.827912030978823], [87.49146938323973, 29.823556019173292], [87.49159812927246, 29.822699686830465], [87.4921989440918, 29.82255075871759], [87.49297142028809, 29.82243906248729], [87.49537467956543, 29.826050510703986], [87.496018409729, 29.827428039043124], [87.49644756317139, 29.82850771014331], [87.49722003936768, 29.83018303875601], [87.49872207641602, 29.83163496749975], [87.50052452087402, 29.83319855947471], [87.49975204467773, 29.835134401350402], [87.49674797058105, 29.837963641231568], [87.4965763092041, 29.83729356534345]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.50112533569336, 29.832900734318393], [87.49743461608887, 29.829699057819667], [87.49743461608887, 29.82865662937959], [87.49322891235352, 29.82173165013024], [87.49374389648436, 29.820838069470422], [87.49340057373047, 29.81964661616466], [87.49348640441895, 29.814359370800148], [87.49468803405762, 29.813391253335432], [87.49709129333496, 29.813093369152803], [87.49975204467773, 29.814806191083832], [87.50103950500488, 29.817040262550112], [87.50189781188965, 29.82039127614494], [87.50344276428223, 29.822774150803713], [87.50481605529785, 29.824188955754668], [87.50773429870605, 29.82552927881331], [87.50756263732909, 29.827018505574348], [87.50593185424803, 29.829550140136895], [87.5049877166748, 29.831337137682596], [87.50267028808594, 29.832230624470732], [87.50112533569336, 29.832900734318393]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.48661994934082, 29.835953400082715], [87.4867057800293, 29.834389851221903], [87.48661994934082, 29.833049647007513], [87.48567581176758, 29.829847975280536], [87.48473167419434, 29.827465269275255], [87.48722076416016, 29.826199433602003], [87.49022483825684, 29.82813541108161], [87.4910831451416, 29.830071351055686], [87.49202728271484, 29.831486052702147], [87.49322891235352, 29.834762126979754], [87.49503135681152, 29.836697938558324], [87.4962329864502, 29.83811254637407], [87.49580383300781, 29.83870816472437], [87.49331474304199, 29.83781473586711], [87.48979568481445, 29.836549031307136], [87.48739242553711, 29.836623484960477], [87.48661994934082, 29.835953400082715]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.49138355255127, 29.828880007817933], [87.49035358428955, 29.827800340740183], [87.48837947845459, 29.826460048139577], [87.48730659484863, 29.825640971590158], [87.48786449432372, 29.824672963377115], [87.48898029327393, 29.823332628834663], [87.49005317687988, 29.822811382769526], [87.49082565307616, 29.822811382769526], [87.49116897583008, 29.824859119530846], [87.49146938323973, 29.826869583896876], [87.49159812927246, 29.82798649106856], [87.49176979064941, 29.828693859154], [87.49138355255127, 29.828880007817933]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.48739242553711, 29.819609383020037], [87.48940944671631, 29.82039127614494], [87.49009609222412, 29.820912534830494], [87.4916410446167, 29.821061465384258], [87.49275684356688, 29.82139655831892], [87.4919843673706, 29.82206674081821], [87.49069690704346, 29.82255075871759], [87.48953819274902, 29.822736918824038], [87.48893737792969, 29.823034774273154], [87.48816490173338, 29.82243906248729], [87.4879503250122, 29.82158272057499], [87.48734951019287, 29.82061467305729], [87.48692035675049, 29.8200189468482], [87.48696327209473, 29.819423217088872], [87.48739242553711, 29.819609383020037]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.4855899810791, 29.82638558691247], [87.48516082763672, 29.82578989509846], [87.4852466583252, 29.82534312390783], [87.48477458953857, 29.824598500818528], [87.48387336730957, 29.823630482508545], [87.48258590698241, 29.822103973047522], [87.48250007629395, 29.82143379079787], [87.48322963714598, 29.82143379079787], [87.48400211334229, 29.82176888248438], [87.48451709747314, 29.821880579463585], [87.48567581176758, 29.82229013398608], [87.48700618743896, 29.822699686830465], [87.4876070022583, 29.822997542390546], [87.48803615570068, 29.823332628834663], [87.48790740966797, 29.823667714155384], [87.48739242553711, 29.824598500818528], [87.4867057800293, 29.825640971590158], [87.4855899810791, 29.82638558691247]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.48125553131102, 29.823444324066394], [87.48185634613037, 29.822736918824038], [87.48232841491699, 29.822587990766603], [87.48288631439209, 29.822736918824038], [87.48310089111328, 29.822923078583752], [87.48348712921143, 29.82374217740741], [87.484130859375, 29.824635732104742], [87.48456001281737, 29.825678202488042], [87.48430252075195, 29.826273894967805], [87.48365879058838, 29.82534312390783], [87.48125553131102, 29.823444324066394]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.47966766357422, 29.824859119530846], [87.47971057891846, 29.824635732104742], [87.47983932495117, 29.82422618719345], [87.48086929321289, 29.823704945788332], [87.48129844665527, 29.82385387218147], [87.4817705154419, 29.824561269518444], [87.48327255249023, 29.825678202488042], [87.48370170593262, 29.826013279944803], [87.4839162826538, 29.826422817532958], [87.48198509216309, 29.82597604917175], [87.47966766357422, 29.824859119530846]]]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[87.4871778488159, 29.82121039571612], [87.48533248901367, 29.820354043277657], [87.48353004455566, 29.819460450302834], [87.48271465301514, 29.818715783388594], [87.48198509216309, 29.81808281215006], [87.48361587524414, 29.81752430478788], [87.48477458953857, 29.817859409579604], [87.48520374298094, 29.818120045863267], [87.48589038848876, 29.818268980577425], [87.48623371124268, 29.818715783388594], [87.48636245727539, 29.819795548604493], [87.48700618743896, 29.820763604054854], [87.4871778488159, 29.82121039571612]]]
}
}]
}
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
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
# 核心代码
/**
* 添加矢量数据
**/
addVecData(){
this.addVecDataByType('polygon',polygonData);
}, /**
* 添加面矢量数据
**/
addVecDataByType(type,data){
let that = this;
let geoJsonObj = data;
let viewer = this._viewer;
let smcPromise = Cesium.GeoJsonDataSource.load(geoJsonObj,
{
clampToGround : true
}
);
smcPromise.then(function (dataSource) {
viewer.dataSources.add(dataSource);
viewer.flyTo(dataSource);
if(type==='polygon'){
that.polygonLayer = dataSource;
}
let entities = dataSource.entities.values;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
entity.polygon.material = Cesium.Color.RED.withAlpha(1)
}
})
},
/**
* 显示透明度滑动条
**/
addOpacityRange(){
$("#opacity").css('display','block')
},
/**
* 更改面透明度
**/
changeLayerOpacity(){
let dataSource = this.polygonLayer;
let opacity = document.getElementById('opacity');
let value = parseFloat(opacity.value);
let entities = dataSource.entities.values;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
entity.polygon.material = Cesium.Color.RED.withAlpha(value)
}
const percent = parseFloat(opacity.value, 2) * 100
opacity.style.backgroundSize = `${percent}%, 100%`
},
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
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
# 完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Use correct character set. -->
<meta charset="utf-8"/>
<!-- Tell IE to use the latest, best version. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/>
<title>cesium-操作面要素透明度</title>
<script src="../lib/Cesium-1.89/Build/Cesium/Cesium.js"></script>
<script src="../../static/lib/vue.min.js"></script>
<script src="../../static/lib/jquery-3.6.0.min.js"></script>
<script src="../../static/data/geodata.js"></script>
<style>
@import url(../lib/Cesium-1.89/Build/Cesium/Widgets/widgets.css);
html,
body, #temp {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
/* 这里不考虑浏览器的兼容性 */
#opacity {
position: absolute;
left:6.5%;
top:82%;
width: 15%;
-webkit-appearance: none;
height: 8px;
border-radius: 4px;
background: -webkit-linear-gradient(#ffa200, #ffa200) no-repeat white;
background-size: 100% 100%; /* 因为周期默认value=0.50正好占50% */
display: none;
}
/* -webkit-slider-thumb仅对谷歌浏览器有效 */
#opacity::-webkit-slider-thumb {
-webkit-appearance: none;
background-color: #aaa;
width: 8px;
height: 20px;
border-radius: 4px;
cursor: pointer;
}
#opacity::-webkit-slider-thumb:hover {
background: #666;
}
</style>
</head>
<body>
<div id="temp">
<div style="display: -webkit-flex;display: flex;width: 100%;height: 100%">
<div style="width: 90%;height: 100%">
<div id="cesiumContainer"></div>
</div>
<div style="width: 10%;height: 100%;background-color: #d3d3d3;padding: 30px">
<button class="btn" @click="addDem">添加DEM</button>
<button class="btn" @click="addVecData">矢量数据</button>
<button class="btn" @click="addOpacityRange">面透明度</button>
<input type="range" id="opacity" min="0" max="1.00" value="1" step="0.01" @change="changeLayerOpacity">
</div>
</div>
</div>
<script>
let EarthComp = new Vue({
el: "#temp",
data: {
_viewer: undefined,
snow:null,//雪
rain:null,//雨
fog:null,//雾
},
mounted: function () {
let that = this;
this.earthInit();
},
methods: {
/**
* 地球初始化
*/
earthInit() {
//天地图token
let TDT_tk = "tdt_token";
//Cesium token
let cesium_tk = "token";
//标注
let TDT_CIA_C = "http://{s}.tianditu.gov.cn/cia_c/wmts?service=wmts&request=GetTile&version=1.0.0" +
"&LAYER=cia&tileMatrixSet=c&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}" +
"&style=default&format=tiles&tk=" + TDT_tk;
// 添加mapbox自定义地图实例
let layer = new Cesium.MapboxStyleImageryProvider({
url: 'https://api.mapbox.com/styles/v1',
username: 'sungang',
styleId: 'styleId',
accessToken: 'accessToken',
scaleFactor: true
});
//初始页面加载
Cesium.Ion.defaultAccessToken = cesium_tk;
let viewer = new Cesium.Viewer('cesiumContainer', {
geocoder: false, // 位置查找工具
baseLayerPicker: false,// 图层选择器(地形影像服务)
timeline: false, // 底部时间线
homeButton: false,// 视角返回初始位置
fullscreenButton: false, // 全屏
animation: false, // 左下角仪表盘(动画器件)
sceneModePicker: false,// 选择视角的模式(球体、平铺、斜视平铺)
navigationHelpButton: false, //导航帮助按钮
imageryProvider: layer
});
//调用影响中文注记服务
viewer.imageryLayers.addImageryProvider(new Cesium.WebMapTileServiceImageryProvider({
url: TDT_CIA_C,
layer: "tdtImg_c",
style: "default",
format: "tiles",
tileMatrixSetID: "c",
subdomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
tilingScheme: new Cesium.GeographicTilingScheme(),
tileMatrixLabels: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"],
maximumLevel: 50,
show: false
}))
this._viewer = viewer;
// 去除版权信息
this._viewer._cesiumWidget._creditContainer.style.display = "none";
},
/**
* 添加模型
*/
addDem() {
let that = this;
let terrainProvider = new Cesium.CesiumTerrainProvider({
url: '../res/data/dem/ASTGTM_N29E087D'
});
that._viewer.terrainProvider = terrainProvider;
that._viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(87.54791, 29.57025, 17863.0),
orientation: {
heading: Cesium.Math.toRadians(0.0),
pitch: Cesium.Math.toRadians(-25.0),
roll: 0.0
}
});
},
/**
* 添加矢量数据
**/
addVecData(){
this.addVecDataByType('polygon',polygonData);
}, /**
* 添加面矢量数据
**/
addVecDataByType(type,data){
let that = this;
let geoJsonObj = data;
let viewer = this._viewer;
let smcPromise = Cesium.GeoJsonDataSource.load(geoJsonObj,
{
clampToGround : true
}
);
smcPromise.then(function (dataSource) {
viewer.dataSources.add(dataSource);
viewer.flyTo(dataSource);
if(type==='polygon'){
that.polygonLayer = dataSource;
}
let entities = dataSource.entities.values;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
entity.polygon.material = Cesium.Color.RED.withAlpha(1)
}
})
},
/**
* 显示透明度滑动条
**/
addOpacityRange(){
$("#opacity").css('display','block')
},
/**
* 更改面透明度
**/
changeLayerOpacity(){
let dataSource = this.polygonLayer;
let opacity = document.getElementById('opacity');
let value = parseFloat(opacity.value);
let entities = dataSource.entities.values;
for (let i = 0; i < entities.length; i++) {
let entity = entities[i];
entity.polygon.material = Cesium.Color.RED.withAlpha(value)
}
const percent = parseFloat(opacity.value, 2) * 100
opacity.style.backgroundSize = `${percent}%, 100%`
},
},
})
</script>
</body>
</html>
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
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
# 实现效果

