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.

DrawPolyline.js 1.6KB

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