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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-29 21:30:41
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { PlotEventType } from '@dc-modules/event'
  7. import { Transform } from '@dc-modules/transform'
  8. import { Rectangle } from '@dc-modules/overlay'
  9. import Draw from './Draw'
  10. const DEF_STYLE = {
  11. material: Cesium.Color.YELLOW.withAlpha(0.6)
  12. }
  13. class DrawRectangle extends Draw {
  14. constructor(style) {
  15. super()
  16. this._maxAnchorSize = 2
  17. this._style = {
  18. ...DEF_STYLE,
  19. ...style
  20. }
  21. }
  22. /**
  23. *
  24. * @private
  25. */
  26. _mountedHook() {
  27. this.drawTool.tooltipMess = '左击选择点位'
  28. this._delegate = new Cesium.Entity({
  29. rectangle: {
  30. ...this._style,
  31. coordinates: new Cesium.CallbackProperty(time => {
  32. if (this._positions.length > 1) {
  33. return Cesium.Rectangle.fromCartesianArray(this._positions)
  34. } else {
  35. return null
  36. }
  37. }, false)
  38. }
  39. })
  40. this._layer.entities.add(this._delegate)
  41. }
  42. /**
  43. *
  44. * @private
  45. */
  46. _stopdHook() {
  47. let rectangle = null
  48. if (this._positions.length) {
  49. rectangle = new Rectangle(
  50. Transform.transformCartesianArrayToWGS84Array(this._positions)
  51. ).setStyle(this._style)
  52. }
  53. this._options.onDrawStop && this._options.onDrawStop(rectangle)
  54. }
  55. /**
  56. *
  57. * @param position
  58. * @private
  59. */
  60. _onDrawAnchor(position) {
  61. let len = this._positions.length
  62. this._positions.push(position)
  63. this.drawTool.fire(PlotEventType.CREATE_ANCHOR, { position })
  64. if (len >= this._maxAnchorSize) {
  65. this._positions.pop()
  66. this.drawTool.fire(PlotEventType.DRAW_STOP)
  67. }
  68. }
  69. }
  70. export default DrawRectangle