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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-29 20:55:14
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { PlotEventType } from '@dc-modules/event'
  7. import { Transform } from '@dc-modules/transform'
  8. import { Polygon } from '@dc-modules/overlay'
  9. import Draw from './Draw'
  10. const DEF_STYLE = {
  11. material: Cesium.Color.YELLOW.withAlpha(0.6),
  12. fill: true
  13. }
  14. class DrawPolygon extends Draw {
  15. constructor(style) {
  16. super()
  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. polygon: {
  30. ...this._style,
  31. hierarchy: new Cesium.CallbackProperty(() => {
  32. if (this._positions.length > 2) {
  33. return new Cesium.PolygonHierarchy(
  34. this._positions.map(item => item.clone())
  35. )
  36. } else {
  37. return null
  38. }
  39. }, false)
  40. }
  41. })
  42. this._layer.entities.add(this._delegate)
  43. }
  44. /**
  45. *
  46. * @private
  47. */
  48. _stopdHook() {
  49. let polygon = null
  50. if (this._positions.length) {
  51. polygon = new Polygon(
  52. Transform.transformCartesianArrayToWGS84Array(this._positions)
  53. ).setStyle(this._style)
  54. }
  55. this._options.onDrawStop && this._options.onDrawStop(polygon)
  56. }
  57. /**
  58. *
  59. * @param position
  60. * @private
  61. */
  62. _onDrawAnchor(position) {
  63. this._positions.push(position)
  64. this.drawTool.fire(PlotEventType.CREATE_ANCHOR, { position })
  65. }
  66. }
  67. export default DrawPolygon