3d9253a70203d09605115753e68f3364599521b3.svn-base 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div>
  3. <div class="btn_box">
  4. <a-radio-group v-model="onTool">
  5. <a-radio-button value="none">
  6. </a-radio-button>
  7. <a-radio-button value="Point">点</a-radio-button>
  8. <a-radio-button value="LineString">线</a-radio-button>
  9. <a-radio-button value="Polygon">面</a-radio-button>
  10. </a-radio-group>
  11. </div>
  12. <div class="clearBtn">
  13. <a-button @click="clearFeature('Point')">清除点</a-button>
  14. <a-button @click="clearFeature('LineString')">清除线</a-button>
  15. <a-button @click="clearFeature('Polygon')">清除面</a-button>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import Style from "ol/style/Style";
  21. import {Draw} from "ol/interaction";
  22. import VectorLayer from "ol/layer/Vector";
  23. import {Vector as VectorSource} from 'ol/source';
  24. import Stroke from "ol/style/Stroke";
  25. import IconStyle from 'ol/style/Icon';
  26. import Fill from "ol/style/Fill";
  27. export default {
  28. name: "drawTool",
  29. data() {
  30. return {
  31. onTool: 'none',
  32. map: {},
  33. draw: {},
  34. toolLayer: {}
  35. };
  36. },
  37. inject: ['baseMap'],
  38. mounted() {
  39. setTimeout(() => {
  40. this.map = this.baseMap.map;
  41. this.initToolLayer()
  42. }, 300);
  43. },
  44. watch: {
  45. onTool(val) {
  46. this.removeInteraction()
  47. this.interaction()
  48. },
  49. },
  50. methods: {
  51. // 初始化工具图层
  52. initToolLayer() {
  53. // 将图形的数据层包上一层图层放入地图
  54. // 点层样式
  55. this.toolLayer.Point = new VectorLayer({
  56. source: new VectorSource({wrapX: false}),
  57. zIndex: 9,
  58. style: new Style({
  59. // 设置线颜色\宽度
  60. image: new IconStyle({
  61. offset: [-0,24], //点图片偏移量
  62. src: require('@/assets/locate-red.png'), // 图片路径
  63. })
  64. })
  65. })
  66. // 线层 样式
  67. this.toolLayer.LineString = new VectorLayer({
  68. source: new VectorSource({wrapX: false}),
  69. zIndex: 9,
  70. style: new Style({
  71. // 设置线颜色\宽度
  72. stroke: new Stroke({
  73. width: 4,
  74. color: "#119aff"
  75. })
  76. })
  77. })
  78. // 图形层 样式
  79. this.toolLayer.Polygon = new VectorLayer({
  80. source: new VectorSource({wrapX: false}),
  81. zIndex: 9,
  82. style: new Style({
  83. // 设置线颜色\宽度
  84. stroke: new Stroke({
  85. width: 4,
  86. color: "#119aff"
  87. }),
  88. // 图形区域内颜色
  89. fill: new Fill({
  90. color: "rgba(57,160,255,0.5)"
  91. })
  92. })
  93. })
  94. // 点线面图层放入地图盒子
  95. for (let k in this.toolLayer) {
  96. this.map.addLayer(this.toolLayer[k])
  97. }
  98. },
  99. // 绘制点线面
  100. interaction() {
  101. if (this.onTool !== 'none') {
  102. //this.onTool的值即为绘制的类型
  103. this.draw = new Draw(
  104. {
  105. source: this.toolLayer[this.onTool].getSource(),
  106. // 绘制类型 点线面
  107. type: this.onTool,
  108. // 绘制时停止点击事件
  109. stopClick: true
  110. }
  111. )
  112. this.map.addInteraction(this.draw)
  113. this.draw.on("drawend", (evt) => {
  114. if (evt.feature != null) {
  115. //通过改变onTool的值间接触发removeInteraction函数 可以结束绘制
  116. // this.onTool = 'none'
  117. // 绘制结束之后 绘制出的点线面数据
  118. let info = evt.feature.getGeometry().getCoordinates()
  119. }
  120. }, this);
  121. }
  122. },
  123. // 手动停止绘制
  124. removeInteraction() {
  125. this.map.removeInteraction(this.draw)
  126. },
  127. // 清除数据
  128. clearFeature(layer) {
  129. this.toolLayer[layer].getSource().clear()
  130. }
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. </style>