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.

CustomLabel.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-07-28 18:37:59
  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 CustomLabel extends Overlay {
  12. constructor(position, text) {
  13. super()
  14. this._delegate = new Cesium.Entity({ label: {} })
  15. this._position = Parse.parsePosition(position)
  16. this._text = text
  17. this.type = Overlay.getOverlayType('custom_label')
  18. this._state = State.INITIALIZED
  19. }
  20. set position(position) {
  21. this._position = Parse.parsePosition(position)
  22. this._delegate.position = Transform.transformWGS84ToCartesian(
  23. this._position
  24. )
  25. return this
  26. }
  27. get position() {
  28. return this._position
  29. }
  30. set text(text) {
  31. this._text = text
  32. this._delegate.label.text = this._text
  33. return this
  34. }
  35. get text() {
  36. return this._text
  37. }
  38. _mountedHook() {
  39. /**
  40. * set the location
  41. */
  42. this.position = this._position
  43. /**
  44. * initialize the Overlay parameter
  45. */
  46. this.text = this._text
  47. }
  48. /**
  49. *
  50. * @param {*} style
  51. */
  52. setStyle(style) {
  53. if (!style || Object.keys(style).length === 0) {
  54. return this
  55. }
  56. delete style['text']
  57. this._style = style
  58. Util.merge(this._delegate.label, this._style)
  59. return this
  60. }
  61. /**
  62. * Sets VLine style
  63. * @param style
  64. * @returns {CustomLabel}
  65. */
  66. setVLine(style = {}) {
  67. if (this._position.alt > 0 && !this._delegate.polyline) {
  68. let position = this._position.copy()
  69. position.alt = style.height || 0
  70. this._delegate.polyline = {
  71. ...style,
  72. positions: Transform.transformWGS84ArrayToCartesianArray([
  73. position,
  74. this._position
  75. ])
  76. }
  77. }
  78. return this
  79. }
  80. /**
  81. * Sets bottom circle
  82. * @param radius
  83. * @param style
  84. * @param rotateAmount
  85. * @returns {CustomLabel}
  86. */
  87. setBottomCircle(radius, style = {}, rotateAmount = 0) {
  88. let stRotation = 0
  89. let amount = rotateAmount
  90. this._delegate.ellipse = {
  91. ...style,
  92. semiMajorAxis: radius,
  93. semiMinorAxis: radius,
  94. stRotation: new Cesium.CallbackProperty(time => {
  95. stRotation += amount
  96. if (stRotation >= 360 || stRotation <= -360) {
  97. stRotation = 0
  98. }
  99. return stRotation
  100. })
  101. }
  102. return this
  103. }
  104. }
  105. Overlay.registerType('custom_label')
  106. export default CustomLabel