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.

Billboard.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-19 10:18:23
  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 { Util } from '@dc-modules/utils'
  9. import { Transform } from '@dc-modules/transform'
  10. import Overlay from '../Overlay'
  11. class Billboard extends Overlay {
  12. constructor(position, icon) {
  13. super()
  14. this._delegate = new Cesium.Entity({ billboard: {} })
  15. this._position = Parse.parsePosition(position)
  16. this._icon = icon
  17. this._size = [32, 32]
  18. this.type = Overlay.getOverlayType('billboard')
  19. this._state = State.INITIALIZED
  20. }
  21. set position(position) {
  22. this._position = Parse.parsePosition(position)
  23. this._delegate.position = Transform.transformWGS84ToCartesian(
  24. this._position
  25. )
  26. return this
  27. }
  28. get position() {
  29. return this._position
  30. }
  31. set icon(icon) {
  32. this._icon = icon
  33. this._delegate.billboard.image = this._icon
  34. return this
  35. }
  36. get icon() {
  37. return this._icon
  38. }
  39. set size(size) {
  40. if (!Array.isArray(size)) {
  41. throw new Error('Billboard: the size invalid')
  42. }
  43. this._size = size
  44. this._delegate.billboard.width = this._size[0] || 32
  45. this._delegate.billboard.height = this._size[1] || 32
  46. return this
  47. }
  48. get size() {
  49. return this._size
  50. }
  51. _mountedHook() {
  52. /**
  53. * set the location
  54. */
  55. this.position = this._position
  56. /**
  57. * initialize the Overlay parameter
  58. */
  59. this.icon = this._icon
  60. this.size = this._size
  61. }
  62. /**
  63. *
  64. * @param style
  65. * @returns {Billboard}
  66. */
  67. setStyle(style) {
  68. if (!style || Object.keys(style).length === 0) {
  69. return this
  70. }
  71. delete style['image'] && delete style['width'] && delete style['height']
  72. this._style = style
  73. Util.merge(this._delegate.billboard, this._style)
  74. return this
  75. }
  76. /**
  77. * Parse from entity
  78. * @param entity
  79. * @returns {any}
  80. */
  81. static fromEntity(entity) {
  82. let billboard = undefined
  83. let now = Cesium.JulianDate.now()
  84. let position = Transform.transformCartesianToWGS84(
  85. entity.position.getValue(now)
  86. )
  87. if (entity.billboard) {
  88. billboard = new Billboard(position, entity.billboard.image.getValue(now))
  89. billboard.attr = {
  90. ...entity?.properties?.getValue(now)
  91. }
  92. }
  93. return billboard
  94. }
  95. }
  96. Overlay.registerType('billboard')
  97. export default Billboard