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.

DrawPolygon.js 1.8KB

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