9baaed4db9891d7f06525bdd7f7307effd0c79ba.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="draw" v-drag="'.draw .ant-card-head'">
  3. <a-card v-show='value' title="标绘" style="width: 272px; box-shadow: 0 0 5px #357ee5;">
  4. <a-icon slot="extra" type="close" style='cursor: pointer; color: #FFFFFF;' @click="$emit('input',false)"/>
  5. <a-radio-group v-model="typeSelected" @change="getTypeSelected">
  6. <a-radio-button value="Point">点</a-radio-button>
  7. <a-radio-button value="LineString">线</a-radio-button>
  8. <a-radio-button value="Polygon">面</a-radio-button>
  9. </a-radio-group>
  10. <a-button style="margin-left: 20px" @click="clearFeature" type="primary">清空</a-button>
  11. <div style="user-select: text;max-height: 40vh;overflow-y: auto;margin-top: 10px">
  12. 标绘结果:{{ this.baseMap.geoinfo }}
  13. </div>
  14. </a-card>
  15. </div>
  16. </template>
  17. <script>
  18. import Style from "ol/style/Style";
  19. import {Draw, Snap} from "ol/interaction";
  20. import VectorLayer from "ol/layer/Vector";
  21. import {Vector as VectorSource} from 'ol/source';
  22. import Stroke from "ol/style/Stroke";
  23. import {GeoJSON} from "ol/format";
  24. import Fill from "ol/style/Fill";
  25. import CircleStyle from "ol/style/Circle";
  26. export default {
  27. name: "drawTool",
  28. data() {
  29. return {
  30. typeSelected: 'None',
  31. drawLayer: null,
  32. draw: null,
  33. map: null,
  34. info: ''
  35. };
  36. },
  37. props: {
  38. value: {
  39. type: Boolean,
  40. default: false,
  41. required: true
  42. }
  43. },
  44. inject: ['baseMap'],
  45. watch: {
  46. value: function (newValue) {
  47. if (newValue === false) {
  48. this.removeInteraction();
  49. this.typeSelected = 'None';
  50. } else {
  51. }
  52. }
  53. },
  54. mounted() {
  55. },
  56. methods: {
  57. initDrawLayer() {
  58. this.map = this.baseMap.map;
  59. if (!this.drawLayer) {
  60. this.drawLayer = new VectorLayer({
  61. //layer所对应的source
  62. source: new VectorSource(),
  63. style: new Style({
  64. fill: new Fill({
  65. color: 'rgba(255, 255, 255, 0.2)',
  66. }),
  67. stroke: new Stroke({
  68. color: 'blue',
  69. width: 2,
  70. }),
  71. image: new CircleStyle({
  72. radius: 7,
  73. fill: new Fill({
  74. color: 'blue',
  75. }),
  76. }),
  77. }),
  78. })
  79. //把layer加入到地图中
  80. this.map.addLayer(this.drawLayer);
  81. }
  82. this.getTypeSelected();
  83. },
  84. getTypeSelected() {
  85. this.map = this.baseMap.map;
  86. this.draw && this.removeInteraction();
  87. //再根据typeSelect的值绘制新的Interaction
  88. this.addInteraction();
  89. },
  90. addInteraction() {
  91. let value = this.baseMap.drawType || this.typeSelected;
  92. if (value !== 'None') {
  93. this.draw = new Draw({
  94. source: this.drawLayer.getSource(),
  95. type: value,
  96. // 绘制时停止点击事件
  97. stopClick: true,
  98. /*style: new Style({
  99. stroke: new Stroke({
  100. color: 'red',
  101. width: 3
  102. })
  103. })*/
  104. });
  105. this.map.addInteraction(this.draw);
  106. this.draw.on("drawstart", (evt) => {
  107. this.clearFeature();
  108. })
  109. this.draw.on("drawend", (evt) => {
  110. if (evt.feature != null) {
  111. //通过改变onTool的值间接触发removeInteraction函数 可以结束绘制
  112. // this.onTool = 'none'
  113. // 绘制结束之后 绘制出的点线面数据
  114. let featureGeoJson = JSON.parse(new GeoJSON().writeFeature(evt.feature));
  115. featureGeoJson.crs = {
  116. "type": "name",
  117. "properties": {
  118. "name": "EPSG:4490"
  119. }
  120. };
  121. this.baseMap.geoinfo = featureGeoJson;
  122. /*this.$notification.open({
  123. message: '标绘完成',
  124. // description:JSON.stringify(featureGeoJson)
  125. });*/
  126. // alert(JSON.stringify(featureGeoJson));
  127. // this.$emit('',featureGeoJson)
  128. }
  129. }, this);
  130. }
  131. },
  132. // 手动停止绘制
  133. removeInteraction() {
  134. this.map.removeInteraction(this.draw)
  135. },
  136. // 清除数据
  137. clearFeature() {
  138. if (this.drawLayer) {
  139. this.drawLayer.getSource().clear();
  140. this.baseMap.geoinfo = null;
  141. }
  142. }
  143. }
  144. }
  145. </script>
  146. <style scoped>
  147. </style>