You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DynamicOverlay.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-05-05 09:16:35
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { Transform } from '@dc-modules/transform'
  9. import Overlay from '../Overlay'
  10. class DynamicOverlay extends Overlay {
  11. constructor() {
  12. super()
  13. this._startTime = undefined
  14. this._lastTime = undefined
  15. this._sampledPosition = new Cesium.SampledPositionProperty()
  16. this._cache = []
  17. this._maxCacheSize = 10
  18. this._state = State.INITIALIZED
  19. }
  20. set maxCacheSize(maxCacheSize) {
  21. this._maxCacheSize = maxCacheSize
  22. return this
  23. }
  24. get maxCacheSize() {
  25. return this._maxCacheSize
  26. }
  27. get position() {
  28. return Transform.transformCartesianToWGS84(
  29. this._sampledPosition.getValue(Cesium.JulianDate.now())
  30. )
  31. }
  32. /**
  33. *
  34. * @private
  35. */
  36. _removePosition() {
  37. if (this._cache.length > this._maxCacheSize) {
  38. let start = Cesium.JulianDate.addSeconds(
  39. this._cache[0],
  40. -0.2,
  41. new Cesium.JulianDate()
  42. )
  43. let stop = Cesium.JulianDate.addSeconds(
  44. this._cache[this._cache.length - this._maxCacheSize],
  45. -0.2,
  46. new Cesium.JulianDate()
  47. )
  48. this._sampledPosition.removeSamples(
  49. new Cesium.TimeInterval({
  50. start: start,
  51. stop: stop
  52. })
  53. )
  54. this._cache.splice(0, this._cache.length - this._maxCacheSize)
  55. }
  56. }
  57. /**
  58. *
  59. * @param position
  60. * @param interval
  61. * @returns {DynamicOverlay}
  62. */
  63. addPosition(position, interval) {
  64. this._removePosition()
  65. let now = Cesium.JulianDate.now()
  66. let time = Cesium.JulianDate.addSeconds(
  67. now,
  68. interval,
  69. new Cesium.JulianDate()
  70. )
  71. this._sampledPosition.addSample(
  72. time,
  73. Transform.transformWGS84ToCartesian(Parse.parsePosition(position))
  74. )
  75. this._lastTime = time
  76. this._cache.push(this._lastTime)
  77. return this
  78. }
  79. /**
  80. *
  81. * @param content
  82. * @returns {DynamicOverlay}
  83. */
  84. bindDom(content) {
  85. return this
  86. }
  87. }
  88. export default DynamicOverlay