@go-home-early/go-home-early98 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @go-home-early/go-home-early98 might be problematic. Click here for more details.

Files changed (56) hide show
  1. package/README.md +28 -16
  2. package/dist/go-home-early.umd.js +5 -5
  3. package/package.json +2 -16
  4. package/src/packages/Cesium/DrawMap/DrawJs/CesiumEntityEdit.js +427 -0
  5. package/src/packages/Cesium/DrawMap/DrawJs/CreatePolygonOnGround.js +202 -0
  6. package/src/packages/Cesium/DrawMap/DrawJs/DrawAttackArrow.js +477 -0
  7. package/src/packages/Cesium/DrawMap/DrawJs/DrawBillboard.js +114 -0
  8. package/src/packages/Cesium/DrawMap/DrawJs/DrawCircle.js +311 -0
  9. package/src/packages/Cesium/DrawMap/DrawJs/DrawCircleTY.js +237 -0
  10. package/src/packages/Cesium/DrawMap/DrawJs/DrawCurve.js +256 -0
  11. package/src/packages/Cesium/DrawMap/DrawJs/DrawPincerArrow.js +584 -0
  12. package/src/packages/Cesium/DrawMap/DrawJs/DrawPoint.js +158 -0
  13. package/src/packages/Cesium/DrawMap/DrawJs/DrawPolygon.js +215 -0
  14. package/src/packages/Cesium/DrawMap/DrawJs/DrawPolyline.js +213 -0
  15. package/src/packages/Cesium/DrawMap/DrawJs/DrawPolylineJT.js +218 -0
  16. package/src/packages/Cesium/DrawMap/DrawJs/DrawProFile.js +314 -0
  17. package/src/packages/Cesium/DrawMap/DrawJs/DrawRectangle.js +215 -0
  18. package/src/packages/Cesium/DrawMap/DrawJs/DrawstraightArrow.js +364 -0
  19. package/src/packages/Cesium/DrawMap/DrawJs/MeasurePolygon.js +337 -0
  20. package/src/packages/Cesium/DrawMap/DrawJs/MeasurePolyline.js +285 -0
  21. package/src/packages/Cesium/DrawMap/DrawJs/ReminderTip.js +78 -0
  22. package/src/packages/Cesium/DrawMap/DrawJs/SlopeAspect.js +436 -0
  23. package/src/packages/Cesium/DrawMap/DrawJs/measureLength.js +177 -0
  24. package/src/packages/Cesium/DrawMap/index.vue +272 -0
  25. package/src/packages/Cesium/components/LocationDialog.vue +283 -0
  26. package/src/packages/Cesium/components/Popup.vue +591 -0
  27. package/src/packages/Cesium/components/PoumianDialog.vue +200 -0
  28. package/src/packages/Cesium/imgs/add-off.png +0 -0
  29. package/src/packages/Cesium/imgs/add.png +0 -0
  30. package/src/packages/Cesium/imgs/circle.png +0 -0
  31. package/src/packages/Cesium/imgs/icon-position-blue.png +0 -0
  32. package/src/packages/Cesium/imgs/line.png +0 -0
  33. package/src/packages/Cesium/imgs/map-delete.png +0 -0
  34. package/src/packages/Cesium/imgs/map.png +0 -0
  35. package/src/packages/Cesium/imgs/orientation-bg.svg +14 -0
  36. package/src/packages/Cesium/imgs/orientation.svg +21 -0
  37. package/src/packages/Cesium/imgs/poi.png +0 -0
  38. package/src/packages/Cesium/imgs/point.png +0 -0
  39. package/src/packages/Cesium/imgs/polygon.png +0 -0
  40. package/src/packages/Cesium/imgs/popup/back-popup-img.png +0 -0
  41. package/src/packages/Cesium/imgs/popup/backimg-btn.png +0 -0
  42. package/src/packages/Cesium/imgs/popup/dialog-bd.png +0 -0
  43. package/src/packages/Cesium/imgs/popup/dialog-btn.png +0 -0
  44. package/src/packages/Cesium/imgs/popup/dialog-ft.png +0 -0
  45. package/src/packages/Cesium/imgs/popup/dialog-hd.png +0 -0
  46. package/src/packages/Cesium/imgs/reduce-off.png +0 -0
  47. package/src/packages/Cesium/index.md +292 -0
  48. package/src/packages/Cesium/index.vue +3830 -0
  49. package/src/packages/Cesium/materials/CircularDiffusionMaterialProperty.js +98 -0
  50. package/src/packages/Cesium/materials/CircularRippleMaterialProperty.js +126 -0
  51. package/src/packages/Cesium/materials/PolylineArrowMaterialProperty.js +109 -0
  52. package/src/packages/Cesium/utils/MeasureHeight.js +261 -0
  53. package/src/packages/Cesium/utils/MeasureTools.js +1188 -0
  54. package/src/packages/index.js +33 -0
  55. package/src/packages/utils/charts.js +54 -0
  56. package/src/packages/utils/index.js +50 -0
@@ -0,0 +1,436 @@
1
+ /*
2
+ * 坡度坡向分析类
3
+ * @Author: Wang jianLei
4
+ * @Date: 2022-04-25 20:21:34
5
+ * @Last Modified by: 宋木桐
6
+ * @Last Modified time: 2024-08-26 11:04:54
7
+ */
8
+ import CreatePolygonOnGround from "./CreatePolygonOnGround";
9
+ import CreateRemindertip from "./ReminderTip";
10
+ import * as turf from "@turf/turf";
11
+ const Cesium = window.Cesium;
12
+ class SlopeAspect {
13
+ constructor(viewer) {
14
+ if (!viewer) throw new Error("no viewer object!");
15
+ this.viewer = viewer;
16
+ this.result = []; //存储创建的坡度分析结果,primitive集合
17
+ this.handler = undefined;
18
+ this.toolTip = "";
19
+ }
20
+ openTip() {
21
+ const $this = this;
22
+ const viewer = this.viewer;
23
+ this.handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
24
+ this.handler.setInputAction(function (movement) {
25
+ let endPos = movement.endPosition;
26
+ var pick = viewer.scene.pick(endPos);
27
+ if (pick && pick.id && pick.id.type === "SlopeAspect") {
28
+ $this.toolTip = pick.id.value;
29
+ CreateRemindertip($this.toolTip, endPos, true);
30
+ } else {
31
+ $this.toolTip = "";
32
+ CreateRemindertip($this.toolTip, endPos, false);
33
+ }
34
+ }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
35
+ }
36
+ closeTip() {
37
+ if (this.handler) {
38
+ this.handler.destroy();
39
+ this.handler = undefined;
40
+ }
41
+ }
42
+ //等距离切分网格
43
+ createNew4Distance(distance) {
44
+ distance = distance || 0.1; //默认0.1km精度
45
+ let width = distance * 200 > 35 ? 35 : distance * 200;
46
+ this.arrowWidth = width < 15 ? 15 : width;
47
+ const $this = this;
48
+ const viewer = this.viewer;
49
+ CreatePolygonOnGround(
50
+ viewer,
51
+ [],
52
+ {
53
+ color: Cesium.Color.RED.withAlpha(0.1),
54
+ outlineColor: Cesium.Color.YELLOW,
55
+ outlineWidth: 2,
56
+ },
57
+ function (polygon) {
58
+ let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
59
+ viewer.entities.remove(polygon);
60
+ let boundary = [];
61
+ let minX = 10000,
62
+ minY = 10000,
63
+ maxX = -10000,
64
+ maxY = -1000;
65
+ for (let index = 0; index < degrees.length; index++) {
66
+ const element = degrees[index];
67
+ const x = element.lng;
68
+ const y = element.lat;
69
+ boundary.push([x, y]);
70
+ minX = x < minX ? x : minX;
71
+ minY = y < minY ? y : minY;
72
+ maxX = x > maxX ? x : maxX;
73
+ maxY = y > maxY ? y : maxY;
74
+ }
75
+ boundary.push(boundary[0]);
76
+ let bbox = [minX, minY, maxX, maxY];
77
+ let mask = turf.polygon([boundary]);
78
+ let gridSquare = turf.squareGrid(bbox, distance, { mask: mask });
79
+ $this.createEllipse(gridSquare);
80
+ }
81
+ );
82
+ }
83
+ // 等分切分网格,切分成一个num*num的网格
84
+ createNew4Num(num) {
85
+ const $this = this;
86
+ const viewer = this.viewer;
87
+ CreatePolygonOnGround(
88
+ viewer,
89
+ [],
90
+ {
91
+ color: Cesium.Color.RED.withAlpha(0.1),
92
+ outlineColor: Cesium.Color.YELLOW,
93
+ outlineWidth: 2,
94
+ },
95
+ function (polygon) {
96
+ let degrees = $this.Cartesian3ListToWGS84(polygon.pottingPoint);
97
+ viewer.entities.remove(polygon);
98
+ let boundary = [];
99
+ let minX = 10000,
100
+ minY = 10000,
101
+ maxX = -10000,
102
+ maxY = -1000;
103
+ for (let index = 0; index < degrees.length; index++) {
104
+ const element = degrees[index];
105
+ const x = element.lng;
106
+ const y = element.lat;
107
+ boundary.push([x, y]);
108
+ minX = x < minX ? x : minX;
109
+ minY = y < minY ? y : minY;
110
+ maxX = x > maxX ? x : maxX;
111
+ maxY = y > maxY ? y : maxY;
112
+ }
113
+ boundary.push(boundary[0]);
114
+ let bbox = [minX, minY, maxX, maxY];
115
+ let a = maxX - minX;
116
+ let b = maxY - minY;
117
+ b = b > a ? b : a;
118
+ const step = b / num;
119
+ let width = step * 2000 > 35 ? 35 : step * 2000;
120
+ $this.arrowWidth = width < 15 ? 15 : width;
121
+ let mask = turf.polygon([boundary]);
122
+ let gridSquare = turf.squareGrid(bbox, step, {
123
+ units: "degrees",
124
+ mask: mask,
125
+ });
126
+ $this.createEllipse(gridSquare);
127
+ }
128
+ );
129
+ }
130
+ createEllipse(gridSquare) {
131
+ let boxResults = [];
132
+ for (let index = 0; index < gridSquare.features.length; index++) {
133
+ const feature = gridSquare.features[index];
134
+ const coordinates = feature.geometry.coordinates[0];
135
+ const centerdegree = [
136
+ (coordinates[0][0] + coordinates[2][0]) / 2,
137
+ (coordinates[0][1] + coordinates[2][1]) / 2,
138
+ ];
139
+ let centerCartographic = Cesium.Cartographic.fromDegrees(
140
+ centerdegree[0],
141
+ centerdegree[1]
142
+ );
143
+ boxResults.push(centerCartographic);
144
+ for (let i = 0; i < coordinates.length; i++) {
145
+ const coord = coordinates[i];
146
+ let cartographic = Cesium.Cartographic.fromDegrees(coord[0], coord[1]);
147
+ boxResults.push(cartographic);
148
+ const coord1 = coordinates[i + 1];
149
+ if (coord1) {
150
+ let newCoord = [
151
+ (coord[0] + coord1[0]) / 2,
152
+ (coord[1] + coord1[1]) / 2,
153
+ ];
154
+ let newCartographic = Cesium.Cartographic.fromDegrees(
155
+ newCoord[0],
156
+ newCoord[1]
157
+ );
158
+ boxResults.push(newCartographic);
159
+ }
160
+ }
161
+ }
162
+ console.log(`output->boxResults,`, boxResults);
163
+ // console.log(
164
+ // `output->this.viewer.terrainProvider`,
165
+ // this.viewer.terrainProvider
166
+ // );
167
+ let that = this;
168
+
169
+ Cesium.sampleTerrain(this.viewer.terrainProvider, 11, boxResults)
170
+ .then(function (updatePositions) {
171
+ console.log(`output->updatePositions`, updatePositions);
172
+ let arrr = [];
173
+ let ellipseResults = updatePositions.reduce(function (
174
+ pre,
175
+ item,
176
+ index,
177
+ updatePositions
178
+ ) {
179
+ var begin = index * 10;
180
+ var end = begin + 10;
181
+ var res = updatePositions.slice(begin, end);
182
+ if (res.length != 0) {
183
+ arrr[index] = res;
184
+ }
185
+ // console.log(`output->arrr`, arrr);
186
+ return arrr;
187
+ },
188
+ []);
189
+ // console.log(`output->ellipseResults`, ellipseResults);
190
+ that.calculateSlope(ellipseResults);
191
+ })
192
+ .catch(function (error) {
193
+ console.error("获取地形高度信息时出错:", error);
194
+ });
195
+
196
+ // Cesium.sampleTerrainMostDetailed(
197
+ // this.viewer.terrainProvider,
198
+ // boxResults
199
+ // ).then((updatePositions) => {
200
+ // let arrr = [];
201
+ // let ellipseResults = updatePositions.reduce(function (
202
+ // pre,
203
+ // item,
204
+ // index,
205
+ // updatePositions
206
+ // ) {
207
+ // var begin = index * 10;
208
+ // var end = begin + 10;
209
+ // var res = updatePositions.slice(begin, end);
210
+ // if (res.length != 0) {
211
+ // arrr[index] = res;
212
+ // }
213
+ // console.log(`output->arrr`, arrr);
214
+ // return arrr;
215
+ // },
216
+ // []);
217
+ // console.log(`output->ellipseResults`, ellipseResults);
218
+ // this.calculateSlope(ellipseResults);
219
+ // });
220
+ }
221
+
222
+ createPolygonInsrance(points, color) {
223
+ let positions = [];
224
+ for (let index = 1; index < points.length - 1; index++) {
225
+ const element = points[index];
226
+ positions.push(Cesium.Cartographic.toCartesian(element));
227
+ }
228
+ let polygon = new Cesium.PolygonGeometry({
229
+ polygonHierarchy: new Cesium.PolygonHierarchy(positions),
230
+ });
231
+
232
+ let polygonInstance = new Cesium.GeometryInstance({
233
+ geometry: polygon,
234
+ attributes: {
235
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
236
+ Cesium.Color.fromCssColorString(color)
237
+ ),
238
+ show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
239
+ },
240
+ });
241
+ return polygonInstance;
242
+ }
243
+ createArrowInstance(
244
+ targetPoint,
245
+ center,
246
+ diagonalPoint,
247
+ heightDifference,
248
+ curSlope
249
+ ) {
250
+ let cartographic_0 = new Cesium.Cartographic(
251
+ (targetPoint.longitude + center.longitude) / 2,
252
+ (targetPoint.latitude + center.latitude) / 2,
253
+ (targetPoint.height + center.height) / 2
254
+ );
255
+ let cartographic_1 = new Cesium.Cartographic(
256
+ (diagonalPoint.longitude + center.longitude) / 2,
257
+ (diagonalPoint.latitude + center.latitude) / 2,
258
+ (diagonalPoint.height + center.height) / 2
259
+ );
260
+ //偏移的
261
+ let positions1 =
262
+ heightDifference > 0
263
+ ? [
264
+ Cesium.Cartographic.toCartesian(cartographic_0),
265
+ Cesium.Cartographic.toCartesian(cartographic_1),
266
+ ]
267
+ : [
268
+ Cesium.Cartographic.toCartesian(cartographic_1),
269
+ Cesium.Cartographic.toCartesian(cartographic_0),
270
+ ];
271
+ //箭头线
272
+ const instance = new Cesium.GeometryInstance({
273
+ id: {
274
+ type: "SlopeAspect",
275
+ value: this.calculateSlopeText(curSlope),
276
+ },
277
+ geometry: new Cesium.GroundPolylineGeometry({
278
+ positions: positions1,
279
+ width: this.arrowWidth,
280
+ }),
281
+ attributes: {
282
+ color: Cesium.ColorGeometryInstanceAttribute.fromColor(
283
+ Cesium.Color.BLUE.withAlpha(0.6)
284
+ ),
285
+ show: new Cesium.ShowGeometryInstanceAttribute(true), //显示或者隐藏
286
+ },
287
+ });
288
+ return instance;
289
+ }
290
+ calculateSlope(ellipseResults) {
291
+ let instances = [];
292
+ let polygonInstance = [];
293
+ for (let index = 0; index < ellipseResults.length; index++) {
294
+ const ellipse = ellipseResults[index];
295
+
296
+ const center = ellipse[0];
297
+ let heightDifference = 0;
298
+ let maxIndex = 0;
299
+ for (let i = 1; i < ellipse.length - 1; i++) {
300
+ const point = ellipse[i];
301
+ let curHD = point.height - center.height;
302
+ if (Math.abs(curHD) > heightDifference) {
303
+ heightDifference = curHD;
304
+ maxIndex = i;
305
+ }
306
+ }
307
+ let pos0 = new Cesium.Cartographic(center.longitude, center.latitude, 0);
308
+ let pos1 = new Cesium.Cartographic(
309
+ ellipse[maxIndex].longitude,
310
+ ellipse[maxIndex].latitude,
311
+ 0
312
+ );
313
+ let distance = Cesium.Cartesian3.distance(
314
+ Cesium.Cartographic.toCartesian(pos0),
315
+ Cesium.Cartographic.toCartesian(pos1)
316
+ );
317
+ let curSlope = Math.abs(heightDifference / distance); //坡度的tan值
318
+ let curColor = this.calculateSlopeColor(curSlope, 0.4);
319
+ const curPolygonInstance = this.createPolygonInsrance(ellipse, curColor);
320
+ polygonInstance.push(curPolygonInstance);
321
+
322
+ let diagonalPoint =
323
+ maxIndex > 4 ? ellipse[maxIndex - 4] : ellipse[maxIndex + 4]; //对角点
324
+ let targetPoint = ellipse[maxIndex];
325
+ const arrowInstance = this.createArrowInstance(
326
+ targetPoint,
327
+ center,
328
+ diagonalPoint,
329
+ heightDifference,
330
+ curSlope
331
+ );
332
+ instances.push(arrowInstance);
333
+ }
334
+ const mapPrimitive = this.viewer.scene.primitives.add(
335
+ new Cesium.GroundPrimitive({
336
+ geometryInstances: polygonInstance,
337
+ appearance: new Cesium.PerInstanceColorAppearance({
338
+ translucent: true, //false时透明度无效
339
+ closed: false,
340
+ }),
341
+ })
342
+ );
343
+ const arrowPrimitive = this.viewer.scene.primitives.add(
344
+ new Cesium.GroundPolylinePrimitive({
345
+ geometryInstances: instances,
346
+ appearance: new Cesium.PolylineMaterialAppearance({
347
+ material: new Cesium.Material({
348
+ fabric: {
349
+ type: "PolylineArrow",
350
+ uniforms: {
351
+ color: new Cesium.Color(1.0, 1.0, 0.0, 0.8),
352
+ },
353
+ },
354
+ }),
355
+ }),
356
+ })
357
+ );
358
+ this.result.push(arrowPrimitive, mapPrimitive);
359
+ }
360
+ clearAll() {
361
+ this.result.forEach((element) => {
362
+ this.viewer.scene.primitives.remove(element);
363
+ });
364
+ this.result = [];
365
+ }
366
+ //根据坡度值赋值颜色
367
+ calculateSlopeColor(value, alpha) {
368
+ // 0°~0.5°为平原0.00872686779075879,rgb(85,182,43)
369
+ // 0.5°~2°为微斜坡0.03492076949174773,rgb(135,211,43)
370
+ // 2°~5°为缓斜坡0.08748866352592401,rgb(204,244,44)
371
+ // 5°~15°为斜坡0.2679491924311227,rgb(245,233,44)
372
+ // 15°~35°为陡坡0.7002075382097097,rgb(255,138,43)
373
+ // 35°~55°为峭坡1.4281480067421144,rgb(255,84,43)
374
+ // 55°~90°为垂直壁,rgb(255,32,43)
375
+ if (value < 0.00872686779075879) {
376
+ return "rgba(85,182,43," + alpha + ")";
377
+ } else if (value < 0.03492076949174773) {
378
+ return "rgba(135,211,43," + alpha + ")";
379
+ } else if (value < 0.08748866352592401) {
380
+ return "rgba(204,244,44," + alpha + ")";
381
+ } else if (value < 0.2679491924311227) {
382
+ return "rgba(245,233,44," + alpha + ")";
383
+ } else if (value < 0.7002075382097097) {
384
+ return "rgba(255,138,43," + alpha + ")";
385
+ } else if (value < 1.4281480067421144) {
386
+ return "rgba(255,84,43," + alpha + ")";
387
+ } else {
388
+ return "rgba(255,32,43," + alpha + ")";
389
+ }
390
+ }
391
+ //根据坡度返回文案
392
+ calculateSlopeText(value) {
393
+ // 0°~0.5°为平原0.00872686779075879,rgb(85,182,43)
394
+ // 0.5°~2°为微斜坡0.03492076949174773,rgb(135,211,43)
395
+ // 2°~5°为缓斜坡0.08748866352592401,rgb(204,244,44)
396
+ // 5°~15°为斜坡0.2679491924311227,rgb(245,233,44)
397
+ // 15°~35°为陡坡0.7002075382097097,rgb(255,138,43)
398
+ // 35°~55°为峭坡1.4281480067421144,rgb(255,84,43)
399
+ // 55°~90°为垂直壁,rgb(255,32,43)
400
+ if (value < 0.00872686779075879) {
401
+ return value.toFixed(2) + "度,属于平原";
402
+ } else if (value < 0.03492076949174773) {
403
+ return value.toFixed(2) + "度,属于微斜坡";
404
+ } else if (value < 0.08748866352592401) {
405
+ return value.toFixed(2) + "度,属于缓斜坡";
406
+ } else if (value < 0.2679491924311227) {
407
+ return value.toFixed(2) + "度,属于斜坡";
408
+ } else if (value < 0.7002075382097097) {
409
+ return value.toFixed(2) + "度,属于陡坡";
410
+ } else if (value < 1.4281480067421144) {
411
+ return value.toFixed(2) + "度,属于峭坡";
412
+ } else {
413
+ return value.toFixed(2) + "度,属于垂直壁";
414
+ }
415
+ }
416
+ /**
417
+ * 笛卡尔坐标数组转WGS84
418
+ * @param {Array} cartesianList 笛卡尔坐标数组
419
+ * @returns {Array} WGS84经纬度坐标数组
420
+ */
421
+ Cartesian3ListToWGS84(cartesianList) {
422
+ let ellipsoid = Cesium.Ellipsoid.WGS84;
423
+ let result = [];
424
+ for (let index = 0; index < cartesianList.length; index++) {
425
+ const cartesian = cartesianList[index];
426
+ let cartographic = ellipsoid.cartesianToCartographic(cartesian);
427
+ result.push({
428
+ lng: Cesium.Math.toDegrees(cartographic.longitude),
429
+ lat: Cesium.Math.toDegrees(cartographic.latitude),
430
+ alt: cartographic.height,
431
+ });
432
+ }
433
+ return result;
434
+ }
435
+ }
436
+ export default SlopeAspect;
@@ -0,0 +1,177 @@
1
+ // measureLine();
2
+ export function measureLine(viewer, Cesium) {
3
+ var handler = new Cesium.ScreenSpaceEventHandler(
4
+ viewer.scene._imageryLayerCollection
5
+ );
6
+ var positions = [];
7
+ var poly = null;
8
+ var distance = 0;
9
+ var cartesian = null;
10
+ var floatingPoint;
11
+ var labelPt;
12
+ handler.setInputAction(function(movement) {
13
+ const ray = viewer.camera.getPickRay(movement.endPosition);
14
+ cartesian = viewer.scene.globe.pick(ray, viewer.scene);
15
+ if (!Cesium.defined(cartesian)) {
16
+ // 跳出地球时异常
17
+ return;
18
+ }
19
+ if (positions.length >= 2) {
20
+ if (!Cesium.defined(poly)) {
21
+ poly = new PolyLinePrimitive(positions);
22
+ } else {
23
+ positions.pop();
24
+ positions.push(cartesian);
25
+ }
26
+ }
27
+ }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
28
+
29
+ handler.setInputAction(function(movement) {
30
+ const ray = viewer.camera.getPickRay(movement.position);
31
+ cartesian = viewer.scene.globe.pick(ray, viewer.scene);
32
+ if (!Cesium.defined(cartesian)) {
33
+ // 跳出地球时异常
34
+ return;
35
+ }
36
+ if (positions.length == 0) {
37
+ positions.push(cartesian.clone());
38
+ }
39
+ positions.push(cartesian);
40
+ // 记录鼠标单击时的节点位置,异步计算贴地距离
41
+ labelPt = positions[positions.length - 1];
42
+ if (positions.length > 2) {
43
+ getSpaceDistance(positions);
44
+ } else if (positions.length == 2) {
45
+ // 在三维场景中添加Label
46
+ floatingPoint = viewer.entities.add({
47
+ name: '空间距离',
48
+ openwindow: false,
49
+ position: labelPt,
50
+ point: {
51
+ pixelSize: 5,
52
+ color: Cesium.Color.RED,
53
+ outlineColor: Cesium.Color.WHITE,
54
+ outlineWidth: 2
55
+ }
56
+ });
57
+ }
58
+ }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
59
+
60
+ handler.setInputAction(function(movement) {
61
+ handler.destroy(); // 关闭事件句柄
62
+ handler = undefined;
63
+ positions.pop(); // 最后一个点无效
64
+ if (positions.length == 1) viewer.entities.remove(floatingPoint);
65
+ }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
66
+
67
+ var PolyLinePrimitive = (function() {
68
+ function _(positions) {
69
+ this.options = {
70
+ name: '直线',
71
+ openwindow: false,
72
+ polyline: {
73
+ show: true,
74
+ positions: [],
75
+ material: Cesium.Color.CHARTREUSE,
76
+ width: 5,
77
+ clampToGround: true
78
+ }
79
+ };
80
+ this.positions = positions;
81
+ this._init();
82
+ }
83
+
84
+ _.prototype._init = function() {
85
+ var _self = this;
86
+ var _update = function() {
87
+ return _self.positions;
88
+ };
89
+ // 实时更新polyline.positions
90
+ this.options.polyline.positions = new Cesium.CallbackProperty(
91
+ _update,
92
+ false
93
+ );
94
+ var addedEntity = viewer.entities.add(this.options);
95
+ };
96
+
97
+ return _;
98
+ })();
99
+
100
+ // 空间两点距离计算函数
101
+ function getSpaceDistance(positions) {
102
+ // 只计算最后一截,与前面累加
103
+ // 因move和鼠标左击事件,最后两个点坐标重复
104
+ var i = positions.length - 3;
105
+ var point1cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
106
+ var point2cartographic = Cesium.Cartographic.fromCartesian(
107
+ positions[i + 1]
108
+ );
109
+ getTerrainDistance(point1cartographic, point2cartographic);
110
+ }
111
+
112
+ function getTerrainDistance(point1cartographic, point2cartographic) {
113
+ var geodesic = new Cesium.EllipsoidGeodesic();
114
+ geodesic.setEndPoints(point1cartographic, point2cartographic);
115
+ var s = geodesic.surfaceDistance;
116
+ var cartoPts = [point1cartographic];
117
+ for (var jj = 1000; jj < s; jj += 1000) {
118
+ // 分段采样计算距离
119
+ var cartoPt = geodesic.interpolateUsingSurfaceDistance(jj);
120
+ cartoPts.push(cartoPt);
121
+ }
122
+ cartoPts.push(point2cartographic);
123
+ // 返回两点之间的距离
124
+ var promise = Cesium.sampleTerrain(viewer.terrainProvider, 8, cartoPts);
125
+ Cesium.when(promise, function(updatedPositions) {
126
+ for (var jj = 0; jj < updatedPositions.length - 1; jj++) {
127
+ var geoD = new Cesium.EllipsoidGeodesic();
128
+ geoD.setEndPoints(updatedPositions[jj], updatedPositions[jj + 1]);
129
+ var innerS = geoD.surfaceDistance;
130
+ innerS = Math.sqrt(
131
+ Math.pow(innerS, 2) +
132
+ Math.pow(
133
+ updatedPositions[jj + 1].height - updatedPositions[jj].height,
134
+ 2
135
+ )
136
+ );
137
+ distance += innerS;
138
+ }
139
+ // 在三维场景中添加Label
140
+ var lon1 =
141
+ viewer.scene.globe.ellipsoid.cartesianToCartographic(labelPt).longitude;
142
+ var lat1 =
143
+ viewer.scene.globe.ellipsoid.cartesianToCartographic(labelPt).latitude;
144
+ var lonLat =
145
+ '(' +
146
+ Cesium.Math.toDegrees(lon1).toFixed(2) +
147
+ ',' +
148
+ Cesium.Math.toDegrees(lat1).toFixed(2) +
149
+ ')';
150
+ var textDisance = distance.toFixed(2) + '米';
151
+ if (distance > 10000) {
152
+ textDisance = (distance / 1000.0).toFixed(2) + '千米';
153
+ }
154
+ floatingPoint = viewer.entities.add({
155
+ name: '贴地距离',
156
+ position: labelPt,
157
+ openwindow: false,
158
+ point: {
159
+ pixelSize: 5,
160
+ color: Cesium.Color.RED,
161
+ outlineColor: Cesium.Color.WHITE,
162
+ outlineWidth: 2
163
+ },
164
+ label: {
165
+ text: textDisance,
166
+ // text: lonLat + textDisance,
167
+ font: '18px sans-serif',
168
+ fillColor: Cesium.Color.GOLD,
169
+ style: Cesium.LabelStyle.FILL_AND_OUTLINE,
170
+ outlineWidth: 2,
171
+ verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
172
+ pixelOffset: new Cesium.Cartesian2(20, -20)
173
+ }
174
+ });
175
+ });
176
+ }
177
+ }