Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EditPolyline.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-30 22:39:34
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { PlotEventType } from '@dc-modules/event'
  7. import { midCartesian } from '@dc-modules/math'
  8. import Edit from './Edit'
  9. class EditPolyline extends Edit {
  10. constructor(overlay) {
  11. super(overlay)
  12. }
  13. /**
  14. *
  15. * @private
  16. */
  17. _mountedHook() {
  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.entities.add(this._delegate)
  26. }
  27. /**
  28. *
  29. * @private
  30. */
  31. _mountAnchor() {
  32. let positions = [].concat(
  33. this._overlay.delegate.polyline.positions.getValue(
  34. Cesium.JulianDate.now()
  35. )
  36. )
  37. for (let i = 0; i < positions.length - 1; i++) {
  38. let mid = midCartesian(positions[i], positions[i + 1])
  39. this._positions.push(positions[i])
  40. this._positions.push(mid)
  41. }
  42. this._positions.push(positions[positions.length - 1])
  43. this._positions.forEach((item, index) => {
  44. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  45. position: item,
  46. index: index,
  47. isMid: index % 2 !== 0
  48. })
  49. })
  50. }
  51. /**
  52. *
  53. * @param pickedAnchor
  54. * @param position
  55. * @returns {boolean}
  56. * @private
  57. */
  58. _onEditAnchorStop({ pickedAnchor, position }) {
  59. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  60. let currentIndex = properties.index
  61. if (properties.isMid) {
  62. let preMidPosition = midCartesian(
  63. this._positions[currentIndex],
  64. this._positions[currentIndex - 1]
  65. )
  66. let nextMidPosition = midCartesian(
  67. this._positions[currentIndex],
  68. this._positions[currentIndex + 1]
  69. )
  70. this._positions.splice(
  71. currentIndex,
  72. 1,
  73. preMidPosition,
  74. position,
  75. nextMidPosition
  76. )
  77. this.editTool.fire(PlotEventType.CLEAR_ANCHOR)
  78. this._positions.forEach((item, index) => {
  79. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  80. position: item,
  81. index: index,
  82. isMid: index % 2 !== 0
  83. })
  84. })
  85. }
  86. }
  87. /**
  88. *
  89. * @param pickedAnchor
  90. * @param position
  91. * @private
  92. */
  93. _onAnchorMoving({ pickedAnchor, position }) {
  94. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  95. let currentIndex = properties.index
  96. this._positions[currentIndex] = position
  97. if (!properties.isMid && this._options.maxAnchorSize > 2) {
  98. let preAnchorIndex = -1
  99. let preMidAnchorIndex = -1
  100. let nextAnchorIndex = -1
  101. let nextMidAnchorIndex = -1
  102. let len = this._positions.length
  103. if (currentIndex === 0) {
  104. nextAnchorIndex = currentIndex + 2
  105. nextMidAnchorIndex = currentIndex + 1
  106. } else if (properties.index === len - 1) {
  107. preAnchorIndex = currentIndex - 2
  108. preMidAnchorIndex = currentIndex - 1
  109. } else {
  110. preAnchorIndex = currentIndex - 2
  111. preMidAnchorIndex = currentIndex - 1
  112. nextAnchorIndex = currentIndex + 2
  113. nextMidAnchorIndex = currentIndex + 1
  114. }
  115. if (preAnchorIndex > 0) {
  116. let preMidPosition = midCartesian(
  117. this._positions[preAnchorIndex],
  118. this._positions[currentIndex]
  119. )
  120. this._positions[preMidAnchorIndex] = preMidPosition
  121. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  122. index: preMidAnchorIndex,
  123. position: preMidPosition
  124. })
  125. }
  126. if (nextAnchorIndex > 0) {
  127. let nextMidPosition = midCartesian(
  128. this._positions[nextAnchorIndex],
  129. this._positions[currentIndex]
  130. )
  131. this._positions[nextMidAnchorIndex] = nextMidPosition
  132. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  133. index: nextMidAnchorIndex,
  134. position: nextMidPosition
  135. })
  136. }
  137. }
  138. }
  139. }
  140. export default EditPolyline