• openlayers+vue的bug


    使用addInteraction添加交互draw绘制,预期removeInteraction删除交互draw绘制时不再绘制,但是删除绘制不起作用,各种找原因,结果把data中的map变量注释掉即可,原因未知。

    1. <template>
    2. <div>
    3. <div id="map" style="position:absolute;width:100vw;height:100vh">
    4. </div>
    5. <div style="position: absolute; left: 50px; top:10px">
    6. <el-button @click="addInteractions">绘制</el-button>
    7. <el-button @click="removeInteractions">取消</el-button>
    8. </div>
    9. </div>
    10. </template>
    11. <script src="./index.js"></script>
    12. <style lang="scss" src="./index.scss" scoped></style>
    1. import "ol/ol.css";
    2. import GeoJSON from 'ol/format/GeoJSON'
    3. import { Map, View } from "ol";
    4. import TileLayer from "ol/layer/Tile";
    5. import { fromLonLat } from "ol/proj";
    6. import { Select, Modify, Draw, Snap } from 'ol/interaction';
    7. import { Tile, Vector as VectorLayer } from 'ol/layer'
    8. import { Point } from 'ol/geom'
    9. import { XYZ, TileWMS, Vector as VectorSource } from 'ol/source'
    10. import MapOne from '@/components/MapOne/index.vue';
    11. export default {
    12. components: {
    13. MapOne
    14. },
    15. data() {
    16. return {
    17. // map: null, // 不要声明,否则removeInteraction不起作用
    18. }
    19. },
    20. computed: {
    21. },
    22. watch: {
    23. },
    24. mounted() {
    25. this.initMap()
    26. },
    27. created() {
    28. },
    29. methods: {
    30. initMap() {
    31. this.map = new Map({
    32. target: "map",
    33. view: new View({
    34. center: fromLonLat([113.53450137499999, 34.44104525]),
    35. zoom: 5
    36. }),
    37. layers: [
    38. new TileLayer({
    39. source: new XYZ({
    40. url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
    41. }),
    42. }),
    43. ],
    44. });
    45. this.selectSource = new VectorSource({
    46. format: new GeoJSON(),
    47. })
    48. var selectLayer = new VectorLayer({
    49. source: this.selectSource,
    50. })
    51. this.map.addLayer(selectLayer)
    52. },
    53. addInteractions() {
    54. this.select = new Select({
    55. wrapX: false,
    56. });
    57. this.modify = new Modify({
    58. features: this.select.getFeatures(),
    59. });
    60. this.draw = new Draw({
    61. type: 'Polygon',
    62. source: this.selectSource,
    63. });
    64. this.snap = new Snap({
    65. source: this.selectSource,
    66. });
    67. this.changeInteractions('draw')
    68. },
    69. removeInteractions() {
    70. this.map.removeInteraction(this.modify);
    71. this.map.removeInteraction(this.select);
    72. this.map.removeInteraction(this.draw);
    73. this.map.removeInteraction(this.select);
    74. },
    75. changeInteractions(value) {
    76. this.removeInteractions();
    77. switch (value) {
    78. case 'draw': {
    79. this.map.addInteraction(this.draw);
    80. this.map.addInteraction(this.snap);
    81. break;
    82. }
    83. case 'modify': {
    84. this.map.addInteraction(this.select);
    85. this.map.addInteraction(this.modify);
    86. this.map.addInteraction(this.snap);
    87. break;
    88. }
    89. default: {
    90. // pass
    91. }
    92. }
    93. },
    94. }
    95. }

  • 相关阅读:
    给程序员准备的“蜜糍”--SOD框架简介
    什么是自然语言处理
    如何克服发票自动化的 4 个最常见障碍
    51、图论-岛屿数量
    【MySQL】MySQL中如何实现分页操作
    适用于大中型银行的云原生技术体系建设方案
    疯了!全网居然有人一次性把Java虚拟机HotSpot 给讲透彻了
    性能测试-Redis
    「Java」Java面试宝典:全面覆盖常见问题和难点解析
    外汇天眼:了解外汇市场交易中的流动性
  • 原文地址:https://blog.csdn.net/dragonzoebai/article/details/134013341