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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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._positions = []
  17. this._radius = 0
  18. this._style = {
  19. ...DEF_STYLE,
  20. ...style
  21. }
  22. }
  23. _mountEntity() {
  24. this._delegate = new Cesium.Entity({
  25. polygon: {
  26. ...this._style,
  27. hierarchy: new Cesium.CallbackProperty(() => {
  28. if (this._positions.length > 1) {
  29. this._radius = Cesium.Cartesian3.distance(
  30. this._positions[0],
  31. this._positions[1]
  32. )
  33. if (this._radius <= 0) {
  34. return null
  35. }
  36. let cep = Cesium.EllipseGeometryLibrary.computeEllipsePositions(
  37. {
  38. center: this._positions[0],
  39. semiMajorAxis: this._radius,
  40. semiMinorAxis: this._radius,
  41. rotation: 0,
  42. granularity: 0.005
  43. },
  44. false,
  45. true
  46. )
  47. let pnts = Cesium.Cartesian3.unpackArray(cep.outerPositions)
  48. pnts.push(pnts[0])
  49. return new Cesium.PolygonHierarchy(pnts)
  50. } else {
  51. return null
  52. }
  53. }, false)
  54. }
  55. })
  56. this._layer.add(this._delegate)
  57. }
  58. _onClick(e) {
  59. let len = this._positions.length
  60. let position = this._clampToGround ? e.surfacePosition : e.position
  61. if (len === 0) {
  62. this._positions.push(position)
  63. this.createAnchor(position, true)
  64. this._floatingAnchor = this.createAnchor(position)
  65. }
  66. this._positions.push(position)
  67. if (len > 0) {
  68. this.createAnchor(position)
  69. }
  70. if (len > 1) {
  71. this._positions.pop()
  72. this.unbindEvent()
  73. let circle = new Circle(
  74. Transform.transformCartesianToWGS84(this._positions[0]),
  75. this._radius
  76. )
  77. circle.setStyle(this._style)
  78. this._plotEvent.raiseEvent(circle)
  79. }
  80. }
  81. _onMouseMove(e) {
  82. this._tooltip.showAt(e.windowPosition, '单击选择点位')
  83. if (this._floatingAnchor) {
  84. let position = this._clampToGround ? e.surfacePosition : e.position
  85. this._floatingAnchor.position.setValue(position)
  86. this._positions.pop()
  87. this._positions.push(position)
  88. }
  89. }
  90. }
  91. export default DrawCircle