当前位置:   article > 正文

openlayers入门(30)修改绘制的图形_openlayers lazy重新绘制

openlayers lazy重新绘制
  1. <template>
  2. <div class="vm">
  3. <h2 class="h-title">修改已绘制的图形</h2>
  4. <div class="tools-x">
  5. <select id="type" v-model="tool">
  6. <option
  7. v-for="item in tools"
  8. :key="item.value"
  9. :value="item.value"
  10. >{{ item.label }}</option
  11. >
  12. </select>
  13. </div>
  14. <div id="map" class="map-x"></div>
  15. </div>
  16. </template>
  17. <script>
  18. import "ol/ol.css";
  19. import { Map, View } from "ol";
  20. import Tile from "ol/layer/Tile";
  21. import XYZ from "ol/source/XYZ";
  22. import LayerVector from "ol/layer/Vector";
  23. import SourceVector from "ol/source/Vector";
  24. import { Draw, Modify, Snap } from "ol/interaction";
  25. import { Style, Fill, Stroke, Circle } from "ol/style";
  26. export default {
  27. data() {
  28. return {
  29. tool: "Circle", // 当前选中的工具
  30. tools: [
  31. // 工具集
  32. {
  33. value: "Point",
  34. label: "点",
  35. },
  36. {
  37. value: "LineString",
  38. label: "线",
  39. },
  40. {
  41. value: "Polygon",
  42. label: "多边形",
  43. },
  44. {
  45. value: "Circle",
  46. label: "圆",
  47. },
  48. {
  49. value: "None",
  50. label: "无",
  51. },
  52. ],
  53. map: null,
  54. draw: null,
  55. source: new SourceVector(),
  56. draw: null,
  57. snap: null,
  58. };
  59. },
  60. watch: {
  61. tool(newVal) {
  62. this.addInteractions();
  63. },
  64. },
  65. methods: {
  66. initMap() {
  67. let raster = new Tile({
  68. source: new XYZ({
  69. url:
  70. "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  71. }),
  72. });
  73. let vector = new LayerVector({
  74. source: this.source,
  75. style: new Style({
  76. fill: new Fill({
  77. color: "rgba(255, 255, 255, 0.2)",
  78. }),
  79. stroke: new Stroke({
  80. color: "#ffcc33",
  81. width: 2,
  82. }),
  83. image: new Circle({
  84. radius: 7,
  85. fill: new Fill({
  86. color: "#ffcc33",
  87. }),
  88. }),
  89. }),
  90. });
  91. this.map = new Map({
  92. target: "map",
  93. layers: [raster, vector],
  94. view: new View({
  95. projection: "EPSG:4326",
  96. center: [115.543045, 45.16871],
  97. zoom: 10,
  98. }),
  99. });
  100. let modify = new Modify({
  101. source: this.source,
  102. insertVertexCondition: () => false, // 如果返回true,可以增加节点
  103. });
  104. this.map.addInteraction(modify);
  105. this.addInteractions();
  106. },
  107. addInteractions() {
  108. if (this.draw !== null) {
  109. this.map.removeInteraction(this.draw);
  110. }
  111. if (this.snap !== null) {
  112. this.map.removeInteraction(this.snap);
  113. }
  114. this.draw = new Draw({
  115. source: this.source,
  116. type: this.tool,
  117. });
  118. this.map.addInteraction(this.draw);
  119. this.snap = new Snap({ source: this.source });
  120. },
  121. },
  122. mounted() {
  123. this.initMap();
  124. },
  125. };
  126. </script>
  127. <style></style>

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号