您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EditPolyline.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.editTool.tooltipMess = '点击锚点移动,右击结束编辑'
  19. this._delegate.polyline.positions = new Cesium.CallbackProperty(() => {
  20. if (this._positions.length > 1) {
  21. return this._positions
  22. } else {
  23. return null
  24. }
  25. }, false)
  26. this._layer.entities.add(this._delegate)
  27. }
  28. /**
  29. *
  30. * @private
  31. */
  32. _mountAnchor() {
  33. let positions = [].concat(
  34. this._overlay.polyline.positions.getValue(Cesium.JulianDate.now())
  35. )
  36. if (this._options.maxAnchorSize > 2) {
  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. } else {
  51. this._positions = positions
  52. this._positions.forEach((item, index) => {
  53. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  54. position: item,
  55. index: index
  56. })
  57. })
  58. }
  59. this._layer.entities.remove(this._overlay)
  60. }
  61. /**
  62. *
  63. * @param pickedAnchor
  64. * @param position
  65. * @returns {boolean}
  66. * @private
  67. */
  68. _onEditAnchorStop({ pickedAnchor, position }) {
  69. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  70. let currentIndex = properties.index
  71. if (properties.isMid) {
  72. let preMidPosition = midCartesian(
  73. this._positions[currentIndex],
  74. this._positions[currentIndex - 1]
  75. )
  76. let nextMidPosition = midCartesian(
  77. this._positions[currentIndex],
  78. this._positions[currentIndex + 1]
  79. )
  80. this._positions.splice(
  81. currentIndex,
  82. 1,
  83. preMidPosition,
  84. position,
  85. nextMidPosition
  86. )
  87. this.editTool.fire(PlotEventType.CLEAR_ANCHOR)
  88. this._positions.forEach((item, index) => {
  89. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  90. position: item,
  91. index: index,
  92. isMid: index % 2 !== 0
  93. })
  94. })
  95. }
  96. }
  97. /**
  98. *
  99. * @param pickedAnchor
  100. * @param position
  101. * @private
  102. */
  103. _onAnchorMoving({ pickedAnchor, position }) {
  104. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  105. let currentIndex = properties.index
  106. this._positions[currentIndex] = position
  107. if (!properties.isMid && this._options.maxAnchorSize > 2) {
  108. let preAnchorIndex = -1
  109. let preMidAnchorIndex = -1
  110. let nextAnchorIndex = -1
  111. let nextMidAnchorIndex = -1
  112. let len = this._positions.length
  113. if (currentIndex === 0) {
  114. nextAnchorIndex = currentIndex + 2
  115. nextMidAnchorIndex = currentIndex + 1
  116. } else if (properties.index === len - 1) {
  117. preAnchorIndex = currentIndex - 2
  118. preMidAnchorIndex = currentIndex - 1
  119. } else {
  120. preAnchorIndex = currentIndex - 2
  121. preMidAnchorIndex = currentIndex - 1
  122. nextAnchorIndex = currentIndex + 2
  123. nextMidAnchorIndex = currentIndex + 1
  124. }
  125. if (preAnchorIndex > 0) {
  126. let preMidPosition = midCartesian(
  127. this._positions[preAnchorIndex],
  128. this._positions[currentIndex]
  129. )
  130. this._positions[preMidAnchorIndex] = preMidPosition
  131. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  132. index: preMidAnchorIndex,
  133. position: preMidPosition
  134. })
  135. }
  136. if (nextAnchorIndex > 0) {
  137. let nextMidPosition = midCartesian(
  138. this._positions[nextAnchorIndex],
  139. this._positions[currentIndex]
  140. )
  141. this._positions[nextMidAnchorIndex] = nextMidPosition
  142. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  143. index: nextMidAnchorIndex,
  144. position: nextMidPosition
  145. })
  146. }
  147. }
  148. this._options.onCalc && this._options.onCalc(this._positions)
  149. }
  150. }
  151. export default EditPolyline