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.

DrawRectangle.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-29 21:30:41
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { Transform } from '@dc-modules/transform'
  7. import { Rectangle } from '@dc-modules/overlay'
  8. import Draw from './Draw'
  9. const DEF_STYLE = {
  10. material: Cesium.Color.YELLOW.withAlpha(0.6)
  11. }
  12. class DrawRectangle extends Draw {
  13. constructor(style) {
  14. super()
  15. this._positions = []
  16. this._style = {
  17. ...DEF_STYLE,
  18. ...style
  19. }
  20. }
  21. _mountEntity() {
  22. this._delegate = new Cesium.Entity({
  23. rectangle: {
  24. ...this._style,
  25. coordinates: new Cesium.CallbackProperty(time => {
  26. if (this._positions.length > 1) {
  27. return Cesium.Rectangle.fromCartesianArray(this._positions)
  28. } else {
  29. return null
  30. }
  31. }, false)
  32. }
  33. })
  34. this._layer.add(this._delegate)
  35. }
  36. _onClick(e) {
  37. let position = this._clampToGround ? e.surfacePosition : e.position
  38. let len = this._positions.length
  39. if (len === 0) {
  40. this._positions.push(position)
  41. this.createAnchor(position)
  42. this._floatingAnchor = this.createAnchor(position)
  43. }
  44. this._positions.push(position)
  45. this.createAnchor(position)
  46. if (len > 1) {
  47. this._positions.pop()
  48. this.unbindEvent()
  49. let rectangle = new Rectangle(
  50. Transform.transformCartesianArrayToWGS84Array(this._positions)
  51. )
  52. rectangle.setStyle(this._style)
  53. this._plotEvent.raiseEvent(rectangle)
  54. }
  55. }
  56. _onMouseMove(e) {
  57. this._tooltip.showAt(e.windowPosition, '左击选择点位')
  58. if (this._floatingAnchor) {
  59. let position = this._clampToGround ? e.surfacePosition : e.position
  60. this._floatingAnchor.position.setValue(position)
  61. this._positions.pop()
  62. this._positions.push(position)
  63. }
  64. }
  65. }
  66. export default DrawRectangle