openlayers 地图未在 vuejs 中显示

2024-04-16

以下代码片段是一个 .vue 文件,它不会生成任何错误,但 openlayers 地图不会显示。

我尝试了 openlayers 的两个 vue 插件,但似乎没有按照我的需要工作。

在 vue 之外,代码可以工作。

我使用 npm install ol 方法来获取 Openlayers 包。

有什么想法我做错了吗?

<template>
  <div id="app">
    <div id="map" class="map" style="width: 100%; height: 300px; border: 2px solid black; background-color: white"></div>
    <router-view/>
  </div>
</template>

<script>
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {Draw, Modify, Snap} from 'ol/interaction.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
import {OSM, Vector as VectorSource} from 'ol/source.js';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js';

import Feature from 'ol/Feature';
import Polygon from 'ol/geom/Polygon';

export default {
  created() {
         var raster = new TileLayer({
        source: new OSM()
      });
      var source = new VectorSource();
      var vector = new VectorLayer({
        source: source,
        style: new Style({
          fill: new Fill({
            color: 'rgba(255, 255, 255, 0.2)'
          }),
          stroke: new Stroke({
            color: '#ffcc33',
            width: 2
          }),
          image: new CircleStyle({
            radius: 7,
            fill: new Fill({
              color: '#ffcc33'
            })
          })
        })
      });
      var feature = new Feature({
        geometry: new Polygon([
          ])
    });
    var vectorSource= new VectorSource({
        features: [feature ]
    });

    var vectorLayer = new VectorLayer({
        source: vectorSource
    });




      var map = new Map({
        layers: [raster, vector, vectorLayer],
        target: 'map',
        view: new View({
          center: [-13041991.514129914,8126540.3085058555],
          zoom: 4
        })
      });


      var modify = new Modify({source: source});
      modify.on('modifyend',function(e){
        // console.log("feature id is",e.features.getArray()[0].getGeometry().getCoordinates()[0]);
        });
      map.addInteraction(modify);
      // console.log(map);
      var draw, snap; // global so we can remove them later
      // var typeSelect = document.getElementById('type');

      function addInteractions() {
        draw = new Draw({
          source: source,
          // type: typeSelect.value
          type: 'polygon'
        });
        draw.on('drawend',function(e){
            // console.log(e.feature.getGeometry().getCoordinates()[0]);
        })
        map.addInteraction(draw);
        snap = new Snap({source: source});
        map.addInteraction(snap);
      }
      addInteractions();


  }
} 

</script>

使用以下方法,

 beforeMount(){
    this.initializeOpenLayerMap();
  },
 methods: {
   initializeOpenLayerMap() {
            var raster = new TileLayer({
    source: new OSM()
  });
  var source = new VectorSource();
  var vector = new VectorLayer({
    source: source,
    style: new Style({
      fill: new Fill({
        color: 'rgba(255, 255, 255, 0.2)'
      }),
      stroke: new Stroke({
        color: '#ffcc33',
        width: 2
      }),
      image: new CircleStyle({
        radius: 7,
        fill: new Fill({
          color: '#ffcc33'
        })
      })
    })
  });
  var feature = new Feature({
    geometry: new Polygon([
      ])
});
var vectorSource= new VectorSource({
    features: [feature ]
});

var vectorLayer = new VectorLayer({
    source: vectorSource
});




  var map = new Map({
    layers: [raster, vector, vectorLayer],
    target: 'map',
    view: new View({
      center: [-13041991.514129914,8126540.3085058555],
      zoom: 4
    })
  });

   }
 }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

openlayers 地图未在 vuejs 中显示 的相关文章

随机推荐