官方教程地址:http://lbs.tianditu.gov.cn/docs/#/sanwei/

天地图的地形加载的效果如下,这里绘制了一个Cesium.CorridorGraphics类型的Entity,来验证地形的起伏效果:

实现代码如下:

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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>天地图三维服务接入</title>
<script
type="text/javascript"
cesium="true"
src="https://api.tianditu.gov.cn/cdn/demo/sanwei/static/cesium/Cesium.js"></script>
<script
type="text/javascript"
cesium="true"
src="https://api.tianditu.gov.cn/cdn/plugins/cesium/Cesium_ext_min.js"></script>
<script
type="text/javascript"
cesium="true"
src="https://api.tianditu.gov.cn/cdn/plugins/cesium/long.min.js"></script>
<script
type="text/javascript"
cesium="true"
src="https://api.tianditu.gov.cn/cdn/plugins/cesium/bytebuffer.min.js"></script>
<script
type="text/javascript"
cesium="true"
src="https://api.tianditu.gov.cn/cdn/plugins/cesium/protobuf.min.js"></script>
<link
rel="stylesheet"
cesium="true"
href="https://api.tianditu.gov.cn/cdn/demo/sanwei/static/cesium/Widgets/widgets.css" />
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}

#cesiumContainer {
height: 100%;
}

#cesiumContainer .cesium-viewer-bottom {
display: none;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<script>
const token = "11c161d1558ad38a25a080da1ab26784";
// 服务域名
const tdtUrl = "https://t{s}.tianditu.gov.cn/";
// 服务负载子域
var subdomains = ["0", "1", "2", "3", "4", "5", "6", "7"];

var viewer = new Cesium.Map("cesiumContainer", {
shouldAnimate: true, //是否允许动画
selectionIndicator: false,
baseLayerPicker: false,
fullscreenButton: false,
geocoder: false,
homeButton: false,
infoBox: false,
sceneModePicker: false,
timeline: false,
navigationHelpButton: false,
navigationInstructionsInitiallyVisible: false,
showRenderLoopErrors: false,
shadows: false,
});

// 抗锯齿
viewer.scene.fxaa = true;
viewer.scene.postProcessStages.fxaa.enabled = false;
// 水雾特效
viewer.scene.globe.showGroundAtmosphere = true;
// 设置最大俯仰角,[-90,0]区间内,默认为-30,单位弧度
viewer.scene.screenSpaceCameraController.constrainedPitch =
Cesium.Math.toRadians(-20);
viewer.scene.screenSpaceCameraController.autoResetHeadingPitch = false;
viewer.scene.screenSpaceCameraController.inertiaZoom = 0.5;
viewer.scene.screenSpaceCameraController.minimumZoomDistance = 50;
viewer.scene.screenSpaceCameraController.maximumZoomDistance = 20000000;
viewer.scene.screenSpaceCameraController.zoomEventTypes = [
Cesium.CameraEventType.RIGHT_DRAG,
Cesium.CameraEventType.WHEEL,
Cesium.CameraEventType.PINCH,
];
viewer.scene.screenSpaceCameraController.tiltEventTypes = [
Cesium.CameraEventType.MIDDLE_DRAG,
Cesium.CameraEventType.PINCH,
{
eventType: Cesium.CameraEventType.LEFT_DRAG,
modifier: Cesium.KeyboardEventModifier.CTRL,
},
{
eventType: Cesium.CameraEventType.RIGHT_DRAG,
modifier: Cesium.KeyboardEventModifier.CTRL,
},
];
// 取消默认的双击事件
viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
);

// 叠加影像服务
var imgMap = new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + "DataServer?T=img_w&x={x}&y={y}&l={z}&tk=" + token,
subdomains: subdomains,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 18,
});
viewer.imageryLayers.addImageryProvider(imgMap);

// 叠加国界服务
var iboMap = new Cesium.UrlTemplateImageryProvider({
url: tdtUrl + "DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=" + token,
subdomains: subdomains,
tilingScheme: new Cesium.WebMercatorTilingScheme(),
maximumLevel: 10,
});
viewer.imageryLayers.addImageryProvider(iboMap);

// 叠加地形服务
var terrainUrls = new Array();

for (var i = 0; i < subdomains.length; i++) {
var url =
tdtUrl.replace("{s}", subdomains[i]) +
"mapservice/swdx?T=elv_c&tk=" +
token;
terrainUrls.push(url);
}

var provider = new Cesium.GeoTerrainProvider({
urls: terrainUrls,
});

viewer.terrainProvider = provider;

// 叠加三维地名服务
var wtfs = new Cesium.GeoWTFS({
viewer,
//三维地名服务,使用wtfs服务
subdomains: subdomains,
metadata: {
boundBox: {
minX: -180,
minY: -90,
maxX: 180,
maxY: 90,
},
minLevel: 1,
maxLevel: 20,
},
depthTestOptimization: true,
dTOElevation: 15000,
dTOPitch: Cesium.Math.toRadians(-70),
aotuCollide: true, //是否开启避让
collisionPadding: [5, 10, 8, 5], //开启避让时,标注碰撞增加内边距,上、右、下、左
serverFirstStyle: true, //服务端样式优先
labelGraphics: {
font: "28px sans-serif",
fontSize: 28,
fillColor: Cesium.Color.WHITE,
scale: 0.5,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 2,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
showBackground: false,
backgroundColor: Cesium.Color.RED,
backgroundPadding: new Cesium.Cartesian2(10, 10),
horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
verticalOrigin: Cesium.VerticalOrigin.TOP,
eyeOffset: Cesium.Cartesian3.ZERO,
pixelOffset: new Cesium.Cartesian2(5, 5),
disableDepthTestDistance: undefined,
},
billboardGraphics: {
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.CENTER,
eyeOffset: Cesium.Cartesian3.ZERO,
pixelOffset: Cesium.Cartesian2.ZERO,
alignedAxis: Cesium.Cartesian3.ZERO,
color: Cesium.Color.WHITE,
rotation: 0,
scale: 1,
width: 18,
height: 18,
disableDepthTestDistance: undefined,
},
});

//三维地名服务,使用wtfs服务
wtfs.getTileUrl = function () {
return (
tdtUrl +
"mapservice/GetTiles?lxys={z},{x},{y}&VERSION=1.0.0&tk=" +
token
);
};

// 三维图标服务
wtfs.getIcoUrl = function () {
return tdtUrl + "mapservice/GetIcon?id={id}&tk=" + token;
};

wtfs.initTDT([
{
x: 6,
y: 1,
level: 2,
boundBox: { minX: 90, minY: 0, maxX: 135, maxY: 45 },
},
{
x: 7,
y: 1,
level: 2,
boundBox: { minX: 135, minY: 0, maxX: 180, maxY: 45 },
},
{
x: 6,
y: 0,
level: 2,
boundBox: { minX: 90, minY: 45, maxX: 135, maxY: 90 },
},
{
x: 7,
y: 0,
level: 2,
boundBox: { minX: 135, minY: 45, maxX: 180, maxY: 90 },
},
{
x: 5,
y: 1,
level: 2,
boundBox: { minX: 45, minY: 0, maxX: 90, maxY: 45 },
},
{
x: 4,
y: 1,
level: 2,
boundBox: { minX: 0, minY: 0, maxX: 45, maxY: 45 },
},
{
x: 5,
y: 0,
level: 2,
boundBox: { minX: 45, minY: 45, maxX: 90, maxY: 90 },
},
{
x: 4,
y: 0,
level: 2,
boundBox: { minX: 0, minY: 45, maxX: 45, maxY: 90 },
},
{
x: 6,
y: 2,
level: 2,
boundBox: { minX: 90, minY: -45, maxX: 135, maxY: 0 },
},
{
x: 6,
y: 3,
level: 2,
boundBox: { minX: 90, minY: -90, maxX: 135, maxY: -45 },
},
{
x: 7,
y: 2,
level: 2,
boundBox: { minX: 135, minY: -45, maxX: 180, maxY: 0 },
},
{
x: 5,
y: 2,
level: 2,
boundBox: { minX: 45, minY: -45, maxX: 90, maxY: 0 },
},
{
x: 4,
y: 2,
level: 2,
boundBox: { minX: 0, minY: -45, maxX: 45, maxY: 0 },
},
{
x: 3,
y: 1,
level: 2,
boundBox: { minX: -45, minY: 0, maxX: 0, maxY: 45 },
},
{
x: 3,
y: 0,
level: 2,
boundBox: { minX: -45, minY: 45, maxX: 0, maxY: 90 },
},
{
x: 2,
y: 0,
level: 2,
boundBox: { minX: -90, minY: 45, maxX: -45, maxY: 90 },
},
{
x: 0,
y: 1,
level: 2,
boundBox: { minX: -180, minY: 0, maxX: -135, maxY: 45 },
},
{
x: 1,
y: 0,
level: 2,
boundBox: { minX: -135, minY: 45, maxX: -90, maxY: 90 },
},
{
x: 0,
y: 0,
level: 2,
boundBox: { minX: -180, minY: 45, maxX: -135, maxY: 90 },
},
]);

const e = viewer.entities.add({
corridor: {
positions: Cesium.Cartesian3.fromDegreesArray([
117.11, 36.2634, 117.09, 36.282, 117.07, 36.282,
]),
width: 2000.0,
material: Cesium.Color.GREEN.withAlpha(0.5),
},
});

viewer.zoomTo(e);

// const e = viewer.entities.add({
// rectangle: {
// coordinates: Cesium.Rectangle.fromDegrees(-122.3, 46.0, -122.0, 46.3),
// material: Cesium.Color.RED.withAlpha(0.5),
// },
// });

// viewer.zoomTo(e);
</script>
</body>
</html>