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.

DrawCircle.js 2.3KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-29 21:24:55
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { PlotEventType } from '@dc-modules/event'
  7. import { Transform } from '@dc-modules/transform'
  8. import { Circle } 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 DrawCircle extends Draw {
  15. constructor(style) {
  16. super()
  17. this._maxAnchorSize = 2
  18. this._radius = 0
  19. this._style = {
  20. ...DEF_STYLE,
  21. ...style
  22. }
  23. }
  24. /**
  25. *
  26. * @private
  27. */
  28. _mountedHook() {
  29. this.drawTool.tooltipMess = '单击选择点位'
  30. this._delegate = new Cesium.Entity({
  31. polygon: {
  32. ...this._style,
  33. hierarchy: new Cesium.CallbackProperty(() => {
  34. if (this._positions.length > 1) {
  35. this._radius = Cesium.Cartesian3.distance(
  36. this._positions[0],
  37. this._positions[1]
  38. )
  39. if (this._radius <= 0) {
  40. return null
  41. }
  42. let cep = Cesium.EllipseGeometryLibrary.computeEllipsePositions(
  43. {
  44. center: this._positions[0],
  45. semiMajorAxis: this._radius,
  46. semiMinorAxis: this._radius,
  47. rotation: 0,
  48. granularity: 0.005
  49. },
  50. false,
  51. true
  52. )
  53. let pnts = Cesium.Cartesian3.unpackArray(cep.outerPositions)
  54. pnts.push(pnts[0])
  55. return new Cesium.PolygonHierarchy(pnts)
  56. } else {
  57. return null
  58. }
  59. }, false)
  60. }
  61. })
  62. this._layer.entities.add(this._delegate)
  63. }
  64. /**
  65. *
  66. * @private
  67. */
  68. _stopdHook() {
  69. let circle = new Circle(
  70. Transform.transformCartesianToWGS84(this._positions[0]),
  71. this._radius
  72. ).setStyle(this._style)
  73. this._options.onDrawStop && this._options.onDrawStop(circle)
  74. }
  75. /**
  76. *
  77. * @param position
  78. * @private
  79. */
  80. _onDrawAnchor(position) {
  81. let len = this._positions.length
  82. this._positions.push(position)
  83. this.drawTool.fire(PlotEventType.CREATE_ANCHOR, {
  84. position,
  85. isCenter: len === 1
  86. })
  87. if (len >= this._maxAnchorSize) {
  88. this._positions.pop()
  89. this.drawTool.fire(PlotEventType.DRAW_STOP)
  90. }
  91. }
  92. }
  93. export default DrawCircle