huweili-cesium 1.2.93 → 1.2.94

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 地图测向:以 RTK/起点为原点,点击目标点绘制方位线并显示方位角(北向 0°,顺时针)
2
+ * 地图测向:点击起点与终点,测量第二点相对于第一点的北向角(北向 0°,顺时针)
3
3
  *
4
4
  * @author huweili
5
5
  * @email czxyhuweili@163.com
@@ -13,12 +13,6 @@ function formatBearingDegrees(deg) {
13
13
  return `${(((deg % 360) + 360) % 360).toFixed(1)}°`
14
14
  }
15
15
 
16
- function formatDistanceMeters(meters) {
17
- if (!Number.isFinite(meters)) return ''
18
- if (meters >= 1000) return `${(meters / 1000).toFixed(2)} km`
19
- return `${meters.toFixed(0)} m`
20
- }
21
-
22
16
  function getGroundCartesianFromScreen(map, screenPosition) {
23
17
  if (!screenPosition) return null
24
18
 
@@ -68,18 +62,7 @@ function computeBearingResult(start, end) {
68
62
  azimuth,
69
63
  azimuthText: formatBearingDegrees(azimuth),
70
64
  distance: dist,
71
- distanceText: formatDistanceMeters(dist),
72
- }
73
- }
74
-
75
- function resolveRtkOrigin(mapStore, mapId) {
76
- const rtk = mapStore.getRtkData(mapId) || {}
77
- const lng = Number(rtk.longitude)
78
- const lat = Number(rtk.latitude)
79
- if (!Number.isFinite(lng) || !Number.isFinite(lat) || (lng === 0 && lat === 0)) {
80
- return null
81
65
  }
82
- return { lng, lat }
83
66
  }
84
67
 
85
68
  export function measureBearing() {
@@ -91,15 +74,19 @@ export function measureBearing() {
91
74
  position: new Cesium.CallbackProperty(() => options.getPosition(), false),
92
75
  label: {
93
76
  text: new Cesium.CallbackProperty(() => options.getText(), false),
94
- font: options.font || '10px Microsoft YaHei',
77
+ font: options.font || 'bold 10px Microsoft YaHei',
95
78
  fillColor: options.fillColor || Cesium.Color.WHITE,
79
+ style: Cesium.LabelStyle.FILL_AND_OUTLINE,
80
+ outlineColor: Cesium.Color.BLACK,
81
+ outlineWidth: 2,
96
82
  showBackground: true,
97
83
  backgroundColor: Cesium.Color.fromCssColorString(options.backgroundColor || 'rgba(0, 0, 0, 0.7)'),
98
84
  backgroundPadding: options.backgroundPadding || new Cesium.Cartesian2(4, 4),
99
85
  verticalOrigin: options.verticalOrigin || Cesium.VerticalOrigin.BOTTOM,
100
86
  horizontalOrigin: options.horizontalOrigin || Cesium.HorizontalOrigin.LEFT,
101
- pixelOffset: options.pixelOffset || new Cesium.Cartesian2(16, -16),
87
+ pixelOffset: options.pixelOffset || new Cesium.Cartesian2(18, -18),
102
88
  disableDepthTestDistance: Number.POSITIVE_INFINITY,
89
+ distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, options.maxDistance || 2000000),
103
90
  },
104
91
  })
105
92
  }
@@ -128,7 +115,6 @@ export function measureBearing() {
128
115
  }
129
116
 
130
117
  const color = Cesium.Color.fromCssColorString(options.color || '#ffd400')
131
- const rtkOrigin = resolveRtkOrigin(mapStore, options.mapId)
132
118
 
133
119
  let startPosition = null
134
120
  let endPosition = null
@@ -143,7 +129,6 @@ export function measureBearing() {
143
129
  let handler = null
144
130
  let deleteHandler = null
145
131
  let isFinished = false
146
- let useRtkOrigin = false
147
132
 
148
133
  const tempIds = {
149
134
  startPoint: `${options.id}_bearing_start`,
@@ -173,22 +158,18 @@ export function measureBearing() {
173
158
  )
174
159
  }
175
160
 
176
- const getMeasureText = () => {
161
+ const getCurrentBearingText = () => {
177
162
  const target = endPosition || floatingPosition
178
- if (!startPosition || !target) return ''
163
+ if (!startPosition || !target) return '--'
179
164
  const start = cartesianToLngLat(startPosition)
180
165
  const end = cartesianToLngLat(target)
181
- const { azimuthText, distanceText } = computeBearingResult(start, end)
182
- return `${azimuthText}\n${distanceText}`
166
+ return computeBearingResult(start, end).azimuthText
183
167
  }
184
168
 
185
169
  const getTipText = () => {
186
- if (!startPosition) return '点击确定测向起点'
187
- if (!endPosition) {
188
- const preview = getMeasureText()
189
- return preview ? `点击确定目标点,当前:${preview.replace('\n', ' / ')}` : '点击确定目标点'
190
- }
191
- return `测向完成:${getMeasureText().replace('\n', ' / ')}`
170
+ if (!startPosition) return '点击确定起点'
171
+ if (!endPosition) return `点击确定终点,当前北向角:${getCurrentBearingText()}`
172
+ return `测向完成,北向角:${getCurrentBearingText()}`
192
173
  }
193
174
 
194
175
  const ensurePreviewEntities = () => {
@@ -197,12 +178,13 @@ export function measureBearing() {
197
178
  id: tempIds.startPoint,
198
179
  position: startPosition,
199
180
  point: {
200
- pixelSize: 7,
181
+ pixelSize: 6,
201
182
  color,
202
183
  outlineColor: Cesium.Color.WHITE,
203
184
  outlineWidth: 1.5,
204
185
  disableDepthTestDistance: Number.POSITIVE_INFINITY,
205
186
  heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
187
+ scaleByDistance: new Cesium.NearFarScalar(1000, 1.5, 5000000, 0.5),
206
188
  },
207
189
  })
208
190
  }
@@ -224,24 +206,42 @@ export function measureBearing() {
224
206
  id: tempIds.label,
225
207
  position: new Cesium.CallbackProperty(() => getMidPosition(), false),
226
208
  label: {
227
- text: new Cesium.CallbackProperty(() => getMeasureText(), false),
228
- font: 'bold 11px Microsoft YaHei',
209
+ text: new Cesium.CallbackProperty(() => `北向角 ${getCurrentBearingText()}`, false),
210
+ font: 'bold 10px Microsoft YaHei',
229
211
  fillColor: Cesium.Color.WHITE,
230
212
  style: Cesium.LabelStyle.FILL_AND_OUTLINE,
231
213
  outlineColor: Cesium.Color.BLACK,
232
214
  outlineWidth: 2,
233
215
  showBackground: true,
234
216
  backgroundColor: Cesium.Color.fromCssColorString('rgba(0, 0, 0, 0.75)'),
235
- backgroundPadding: new Cesium.Cartesian2(6, 4),
217
+ backgroundPadding: new Cesium.Cartesian2(4, 4),
236
218
  verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
237
219
  horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
238
- pixelOffset: new Cesium.Cartesian2(0, -14),
220
+ pixelOffset: new Cesium.Cartesian2(0, -16),
239
221
  disableDepthTestDistance: Number.POSITIVE_INFINITY,
222
+ distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 2000000),
240
223
  },
241
224
  })
242
225
  }
243
226
  }
244
227
 
228
+ const createEndPointEntity = () => {
229
+ if (endPointEntity || !endPosition) return
230
+ endPointEntity = map.entities.add({
231
+ id: tempIds.endPoint,
232
+ position: endPosition,
233
+ point: {
234
+ pixelSize: 6,
235
+ color,
236
+ outlineColor: Cesium.Color.WHITE,
237
+ outlineWidth: 1.5,
238
+ disableDepthTestDistance: Number.POSITIVE_INFINITY,
239
+ heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
240
+ scaleByDistance: new Cesium.NearFarScalar(1000, 1.5, 5000000, 0.5),
241
+ },
242
+ })
243
+ }
244
+
245
245
  const removeEntities = () => {
246
246
  [startPointEntity, endPointEntity, lineEntity, labelEntity, deleteButtonEntity, tipEntity].forEach((entity) => {
247
247
  if (entity) map.entities.remove(entity)
@@ -267,6 +267,15 @@ export function measureBearing() {
267
267
  mapStore.removeGraphicMap(options.id, options.mapId)
268
268
  }
269
269
 
270
+ const cancel = () => {
271
+ if (isFinished) return
272
+ if (handler) {
273
+ handler.destroy()
274
+ handler = null
275
+ }
276
+ removeEntities()
277
+ }
278
+
270
279
  const finalize = () => {
271
280
  if (isFinished || !startPosition || !endPosition) return null
272
281
  isFinished = true
@@ -279,18 +288,7 @@ export function measureBearing() {
279
288
  tipEntity = null
280
289
  }
281
290
 
282
- endPointEntity = map.entities.add({
283
- id: tempIds.endPoint,
284
- position: endPosition,
285
- point: {
286
- pixelSize: 7,
287
- color,
288
- outlineColor: Cesium.Color.WHITE,
289
- outlineWidth: 1.5,
290
- disableDepthTestDistance: Number.POSITIVE_INFINITY,
291
- heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
292
- },
293
- })
291
+ createEndPointEntity()
294
292
 
295
293
  deleteButtonEntity = map.entities.add({
296
294
  id: tempIds.deleteButton,
@@ -319,7 +317,7 @@ export function measureBearing() {
319
317
 
320
318
  const start = cartesianToLngLat(startPosition)
321
319
  const end = cartesianToLngLat(endPosition)
322
- const result = { id: options.id, start, end, useRtkOrigin, ...computeBearingResult(start, end) }
320
+ const result = { id: options.id, start, end, ...computeBearingResult(start, end) }
323
321
 
324
322
  mapStore.setGraphicMap(options.id, {
325
323
  id: options.id,
@@ -332,13 +330,6 @@ export function measureBearing() {
332
330
  return result
333
331
  }
334
332
 
335
- if (rtkOrigin) {
336
- startPosition = Cesium.Cartesian3.fromDegrees(rtkOrigin.lng, rtkOrigin.lat, 0)
337
- floatingPosition = startPosition
338
- useRtkOrigin = true
339
- ensurePreviewEntities()
340
- }
341
-
342
333
  tipEntity = createMouseTipEntity(map, {
343
334
  id: tempIds.tip,
344
335
  getPosition: () => cursorPosition || endPosition || floatingPosition || startPosition,
@@ -363,25 +354,26 @@ export function measureBearing() {
363
354
  if (!startPosition) {
364
355
  startPosition = cartesian
365
356
  floatingPosition = cartesian
357
+ cursorPosition = cartesian
366
358
  ensurePreviewEntities()
367
359
  return
368
360
  }
369
361
 
370
362
  endPosition = cartesian
371
363
  floatingPosition = null
364
+ createEndPointEntity()
372
365
  finalize()
373
366
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
374
367
 
375
368
  handler.setInputAction(() => {
376
- if (!isFinished) destroyGraphic()
369
+ cancel()
377
370
  }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
378
371
 
379
- return { id: options.id, finish: finalize, cancel: destroyGraphic }
372
+ return { id: options.id, finish: finalize, cancel, destroy: destroyGraphic }
380
373
  }
381
374
 
382
375
  return {
383
376
  startBearingMeasure,
384
377
  formatBearingDegrees,
385
- formatDistanceMeters,
386
378
  }
387
379
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "huweili-cesium",
3
- "version": "1.2.93",
3
+ "version": "1.2.94",
4
4
  "description": "基于 Cesium 的地图工具库(无人机态势、轨迹、围栏、工具栏等)",
5
5
  "type": "module",
6
6
  "main": "./index.js",