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.

CustomBillboard.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-02-12 21:44:24
  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 CustomBillboard 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._state = State.INITIALIZED
  19. }
  20. get type() {
  21. return Overlay.getOverlayType('custom_billboard')
  22. }
  23. set position(position) {
  24. this._position = Parse.parsePosition(position)
  25. this._delegate.position = Transform.transformWGS84ToCartesian(
  26. this._position
  27. )
  28. return this
  29. }
  30. get position() {
  31. return this._position
  32. }
  33. set icon(icon) {
  34. this._icon = icon
  35. this._delegate.billboard.image = this._icon
  36. return this
  37. }
  38. get icon() {
  39. return this._icon
  40. }
  41. set size(size) {
  42. if (!Array.isArray(size)) {
  43. throw new Error('CustomBillboard: the size invalid')
  44. }
  45. this._size = size
  46. this._delegate.billboard.width = this._size[0] || 32
  47. this._delegate.billboard.height = this._size[1] || 32
  48. return this
  49. }
  50. get size() {
  51. return this._size
  52. }
  53. _mountedHook() {
  54. /**
  55. * set the location
  56. */
  57. this.position = this._position
  58. /**
  59. * initialize the Overlay parameter
  60. */
  61. this.icon = this._icon
  62. this.size = this._size
  63. }
  64. /**
  65. * Sets label
  66. * @param text
  67. * @param textStyle
  68. * @returns {CustomBillboard}
  69. */
  70. setLabel(text, textStyle) {
  71. this._delegate.label = {
  72. ...textStyle,
  73. text: text
  74. }
  75. return this
  76. }
  77. /**
  78. * Sets Style
  79. * @param style
  80. * @returns {CustomBillboard}
  81. */
  82. setStyle(style) {
  83. if (!style || Object.keys(style).length === 0) {
  84. return this
  85. }
  86. delete style['image'] && delete style['width'] && delete style['height']
  87. Util.merge(this._style, style)
  88. Util.merge(this._delegate.billboard, style)
  89. return this
  90. }
  91. /**
  92. * Sets VLine style
  93. * @param style
  94. * @returns {CustomBillboard}
  95. */
  96. setVLine(style = {}) {
  97. if (this._position.alt > 0 && !this._delegate.polyline) {
  98. let position = this._position.copy()
  99. position.alt = style.height || 0
  100. this._delegate.polyline = {
  101. ...style,
  102. positions: Transform.transformWGS84ArrayToCartesianArray([
  103. position,
  104. this._position
  105. ])
  106. }
  107. }
  108. return this
  109. }
  110. /**
  111. * @param {*} radius
  112. * @param {*} style
  113. * @param {*} rotateAmount
  114. */
  115. setBottomCircle(radius, style = {}, rotateAmount = 0) {
  116. let stRotation = 0
  117. let amount = rotateAmount
  118. this._delegate.ellipse = {
  119. ...style,
  120. semiMajorAxis: radius,
  121. semiMinorAxis: radius,
  122. stRotation: new Cesium.CallbackProperty(() => {
  123. stRotation += amount
  124. if (stRotation >= 360 || stRotation <= -360) {
  125. stRotation = 0
  126. }
  127. return stRotation
  128. }, false)
  129. }
  130. return this
  131. }
  132. }
  133. Overlay.registerType('custom_billboard')
  134. export default CustomBillboard