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.

EditPolyline.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-30 22:39:34
  4. */
  5. import Edit from './Edit'
  6. const { Transform } = DC
  7. const { Cesium } = DC.Namespace
  8. class EditPolyline extends Edit {
  9. constructor(overlay) {
  10. super()
  11. this._overlay = overlay
  12. this._positions = []
  13. }
  14. _mountEntity() {
  15. this._delegate = new Cesium.Entity()
  16. this._delegate.merge(this._overlay.delegate)
  17. this._overlay.show = false
  18. this._delegate.polyline.positions = new Cesium.CallbackProperty(() => {
  19. if (this._positions.length > 1) {
  20. return this._positions
  21. } else {
  22. return null
  23. }
  24. }, false)
  25. this._layer.add(this._delegate)
  26. }
  27. _mountAnchor() {
  28. let positions = [].concat(
  29. this._overlay.delegate.polyline.positions.getValue(
  30. Cesium.JulianDate.now()
  31. )
  32. )
  33. for (let i = 0; i < positions.length - 1; i++) {
  34. let mid = this.computeMidPosition(positions[i], positions[i + 1])
  35. this._positions.push(positions[i])
  36. this._positions.push(mid)
  37. }
  38. this._positions.push(positions[positions.length - 1])
  39. this._positions.forEach((item, index) => {
  40. this.createAnchor(item, index, index % 2 !== 0)
  41. })
  42. }
  43. _onClick(e) {
  44. if (this._isMoving) {
  45. this._isMoving = false
  46. if (this._pickedAnchor && this._pickedAnchor.position) {
  47. let position = this._clampToGround ? e.surfacePosition : e.position
  48. this._pickedAnchor.position.setValue(position)
  49. let properties = this._pickedAnchor.properties.getValue(
  50. Cesium.JulianDate.now()
  51. )
  52. if (properties.isMid) {
  53. let preMidPosition = this.computeMidPosition(
  54. this._positions[properties.index],
  55. this._positions[properties.index - 1]
  56. )
  57. let nextMidPosition = this.computeMidPosition(
  58. this._positions[properties.index],
  59. this._positions[properties.index + 1]
  60. )
  61. this._anchorLayer.removeAll()
  62. this._anchors = []
  63. this._positions.splice(
  64. properties.index,
  65. 1,
  66. preMidPosition,
  67. position,
  68. nextMidPosition
  69. )
  70. this._positions.forEach((item, index) => {
  71. this.createAnchor(item, index, index % 2 !== 0)
  72. })
  73. }
  74. }
  75. } else {
  76. this._isMoving = true
  77. if (!e.target.id) {
  78. return false
  79. }
  80. this._pickedAnchor = e.target.id
  81. }
  82. }
  83. _onMouseMove(e) {
  84. this._tooltip.showAt(e.windowPosition, '点击锚点移动,右击结束编辑')
  85. if (!this._isMoving) {
  86. return
  87. }
  88. if (this._pickedAnchor && this._pickedAnchor.position) {
  89. let properties = this._pickedAnchor.properties.getValue(
  90. Cesium.JulianDate.now()
  91. )
  92. let position = this._clampToGround ? e.surfacePosition : e.position
  93. this._pickedAnchor.position.setValue(position)
  94. this._positions[properties.index] = position
  95. if (!properties.isMid) {
  96. let currentIndex = properties.index
  97. let preAnchorIndex = -1
  98. let preMidAnchorIndex = -1
  99. let nextAnchorIndex = -1
  100. let nextMidAnchorIndex = -1
  101. let len = this._positions.length
  102. if (currentIndex === 0) {
  103. nextAnchorIndex = currentIndex + 2
  104. nextMidAnchorIndex = currentIndex + 1
  105. } else if (properties.index === len - 1) {
  106. preAnchorIndex = currentIndex - 2
  107. preMidAnchorIndex = currentIndex - 1
  108. } else {
  109. preAnchorIndex = currentIndex - 2
  110. preMidAnchorIndex = currentIndex - 1
  111. nextAnchorIndex = currentIndex + 2
  112. nextMidAnchorIndex = currentIndex + 1
  113. }
  114. if (preAnchorIndex > 0) {
  115. let preMidPosition = this.computeMidPosition(
  116. this._positions[preAnchorIndex],
  117. this._positions[currentIndex]
  118. )
  119. this._positions[preMidAnchorIndex] = preMidPosition
  120. this._anchors[preMidAnchorIndex].position.setValue(preMidPosition)
  121. }
  122. if (nextAnchorIndex > 0) {
  123. let nextMidPosition = this.computeMidPosition(
  124. this._positions[nextAnchorIndex],
  125. this._positions[currentIndex]
  126. )
  127. this._positions[nextMidAnchorIndex] = nextMidPosition
  128. this._anchors[nextMidAnchorIndex].position.setValue(nextMidPosition)
  129. }
  130. }
  131. }
  132. }
  133. _onRightClick(e) {
  134. this.unbindEvent()
  135. this._overlay.positions = Transform.transformCartesianArrayToWGS84Array(
  136. this._positions
  137. )
  138. this._overlay.show = true
  139. this._plotEvent.raiseEvent(this._overlay)
  140. }
  141. }
  142. export default EditPolyline