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

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