drawFeature.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // import ol from "ol";
  2. import ol from '../js/ol'
  3. class createDrawFeature {
  4. constructor(options, map) {
  5. this.type = options.type || "LineString" // 类型有"Point","Polygon"
  6. this.name = options.name || undefined
  7. this.pointRadius = options.pointRadius || 6
  8. this.pointColor = options.pointColor || 'red'
  9. this.lineWidth = options.lineWidth || 6
  10. this.lineColor = options.lineColor || 'pink'
  11. this.polWidth = options.polWidth || 6
  12. this.polStrColor = options.polStrColor || 'yellow'
  13. this.polColor = options.polColor || '#490b0b'
  14. this.isHand = options.isHand || false //是否手绘
  15. this.isMeasure = options.isMeasure || false // 默认不开启
  16. this.alpha = options.alpha || 0.5
  17. this.map = map
  18. this.style = null
  19. this.source = new ol.source.Vector({wrapX: false,zIndex:999});
  20. this.data = []
  21. this.labelArr = [] // 存放label
  22. this.draw = null
  23. this.length = 0
  24. this.event = undefined
  25. this.vectorLayer = new ol.layer.Vector({
  26. source: this.source,
  27. zIndex:999
  28. });
  29. this.map.addLayer(this.vectorLayer)
  30. }
  31. // 开启画图功能
  32. addInteraction() {
  33. this.draw = new ol.interaction.Draw({
  34. source: this.source,
  35. type: this.type,
  36. style: this.setShapeStyle(this.type),
  37. stopClick: !this.isMeasure,
  38. freehand: this.isHand
  39. });
  40. let that = this;
  41. this.draw.on('drawend', function (evt) {
  42. that.positions = []
  43. const geom = evt.feature.getGeometry();
  44. evt.feature.setStyle(that.setShapeStyle(that.type))
  45. geom.setProperties({type: that.type, name: that.name})
  46. let points = geom.getCoordinates();
  47. that.data.push({
  48. name: that.name,
  49. coordinates: points,
  50. type: geom.getType(),
  51. style: that.style[that.type],
  52. })
  53. that.draw.finishDrawing()
  54. }, this);
  55. this.map.addInteraction(this.draw);
  56. }
  57. setShapeStyle(type) {
  58. let rgb = this.convertHexToRGB(this.polColor)
  59. this.style = {
  60. 'Point': new ol.style.Style({
  61. image: new ol.style.Circle({
  62. radius: this.pointRadius,
  63. fill: new ol.style.Fill({color: this.pointColor}),
  64. }),
  65. }),
  66. 'LineString': new ol.style.Style({
  67. stroke: new ol.style.Stroke({
  68. width: this.lineWidth,
  69. color: this.lineColor,
  70. }),
  71. }),
  72. 'Polygon': new ol.style.Style({
  73. stroke: new ol.style.Stroke({
  74. width: this.polWidth,
  75. color: this.polStrColor,
  76. }),
  77. fill: new ol.style.Fill({color: "rgba("+rgb[0]+","+rgb[1]+","+rgb[2]+","+this.alpha+")"}),
  78. })
  79. }
  80. return this.style[type]
  81. }
  82. convertHexToRGB(str){
  83. str = str[0] === '#' ? str.slice(1) : str
  84. str = str.length === 3 ? str.repeat(2) : str
  85. if ((str.length !== 6) || !(/^[0-9a-fA-F]{3,6}$/i.test(str))) return 'Invalid data'
  86. return [parseInt(str[0] + str[1], 16), parseInt(str[2] + str[3], 16), parseInt(str[4] + str[5], 16)]
  87. }
  88. // 关闭画图功能
  89. closeInteraction() {
  90. this.map.removeInteraction(this.draw);
  91. ol.observable.unByKey(this.event)
  92. this.isHand = false
  93. this.isMeasure = false
  94. }
  95. // 将画图数据返回给用户
  96. getData() {
  97. return this.data
  98. }
  99. //清空图层
  100. clearShapes() {
  101. //this.source.clear()
  102. this.vectorLayer.getSource().clear();
  103. // this.source = [];
  104. // this.source.clear()
  105. // this.map.removeLayer(this.vectorLayer)
  106. // 清除存放overlay的数组
  107. for (let i = 0; i < this.labelArr.length; i++) {
  108. this.map.removeOverlay(this.labelArr[i])
  109. }
  110. }
  111. deleteLastShapes() {
  112. let features = this.source.getFeatures()
  113. if (features.length !== 0) {
  114. let id = features.length - 1
  115. this.source.removeFeature(features[id])
  116. }
  117. }
  118. }
  119. export {createDrawFeature};