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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-29 21:24:55
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { Transform } from '@dc-modules/transform'
  7. import { Circle } 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 DrawCircle extends Draw {
  14. constructor(style) {
  15. super()
  16. this._radius = 0
  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 > 1) {
  28. this._radius = Cesium.Cartesian3.distance(
  29. this._positions[0],
  30. this._positions[1]
  31. )
  32. if (this._radius <= 0) {
  33. return null
  34. }
  35. let cep = Cesium.EllipseGeometryLibrary.computeEllipsePositions(
  36. {
  37. center: this._positions[0],
  38. semiMajorAxis: this._radius,
  39. semiMinorAxis: this._radius,
  40. rotation: 0,
  41. granularity: 0.005
  42. },
  43. false,
  44. true
  45. )
  46. let pnts = Cesium.Cartesian3.unpackArray(cep.outerPositions)
  47. pnts.push(pnts[0])
  48. return new Cesium.PolygonHierarchy(pnts)
  49. } else {
  50. return null
  51. }
  52. }, false)
  53. }
  54. })
  55. this._layer.add(this._delegate)
  56. }
  57. _onClick(e) {
  58. let len = this._positions.length
  59. let position = this._clampToGround ? e.surfacePosition : e.position
  60. if (!position) {
  61. return false
  62. }
  63. if (len === 0) {
  64. this._positions.push(position)
  65. this.createAnchor(position, true)
  66. this._floatingAnchor = this.createAnchor(position)
  67. }
  68. this._positions.push(position)
  69. if (len > 0) {
  70. this.createAnchor(position)
  71. }
  72. if (len > 1) {
  73. this._positions.pop()
  74. this.unbindEvent()
  75. let circle = new Circle(
  76. Transform.transformCartesianToWGS84(this._positions[0]),
  77. this._radius
  78. )
  79. circle.setStyle(this._style)
  80. this._plotEvent.raiseEvent(circle)
  81. }
  82. }
  83. }
  84. export default DrawCircle