vite+vue3项目上openlayers的地图显示问题

问题原因

DOM操作和数据获取的冲突。

vue文件原代码如下

<template>
  <div>
    <h1>地物显示</h1>
    <router-view />
  </div>
    <div id="maplayer" class="map" ></div>
    <div id="scale-bar" class="ol-scale-line ol-unselectable">
     <div class="ol-scale-line-inner"></div>
    </div>
</template>
  
<script setup>
  import Map from 'ol/Map.js'
  import View from 'ol/View.js';
  import TileLayer from 'ol/layer/Tile.js';

  import { TileSuperMapRest } from '@supermapgis/iclient-ol'
  import { ScaleLine } from '@supermapgis/iclient-ol';


  import { onMounted } from 'vue';  

  // 样式导入
  import '@supermapgis/iclient-ol/css/index';
  import '@supermapgis/iclient-common/css/index';
  import 'ol/control';
  import 'ol/control/ScaleLine'

  // 地图实例化函数
  const initMap = () => {
  // const url = "http://localhost:8090/iserver/services/map-chengdu/rest/maps/chengdu_sheng%40chengdu";
  // const url = "http://localhost:8090/iserver/services/map-cd_data/rest/maps/chengdu_alldata";
  const url = "https://iserver.supermap.io/iserver/services/map-world/rest/maps/World"
  const map = new Map({
      layers: [
          new TileLayer({
              source: new TileSuperMapRest({url: url,wrapX: true,}),
              projection: "EPSG:4326",
        }),
      ],
      view: new View({
          center: [104.08 , 30.75],
          zoom: 2,
          projection:'EPSG:4326'
      }),
      target: 'maplayer',
      controls:[
        new ScaleLine({
          className:'ol-scale-line',
          target:document.getElementById('scale-bar'),
          minWidth:64,
          units:'metric'
      })
      ]
  });
  initMap();
  }
</script>

页面无报错但显示空白(挂载地图容器已经设置大小的情况下)。

解决

使用onMounted生命周期钩子,它在组件的DOM已经被挂载到页面上之后被调用。

<template>
  <div>
    <h1>地物显示</h1>
    <router-view />
  </div>
    <div id="maplayer" class="map" ></div>
    <div id="scale-bar" class="ol-scale-line ol-unselectable">
    <div class="ol-scale-line-inner"></div>
    </div>
</template>
  
<script setup>
  import Map from 'ol/Map.js'
  import View from 'ol/View.js';
  import TileLayer from 'ol/layer/Tile.js';

  import { TileSuperMapRest } from '@supermapgis/iclient-ol'
  import { ScaleLine } from '@supermapgis/iclient-ol';

  import { onMounted } from 'vue';  

  // 样式导入
  import '@supermapgis/iclient-ol/css/index';
  import '@supermapgis/iclient-common/css/index';
  import 'ol/control';
  import 'ol/control/ScaleLine'

  // 等待DOM渲染,通过onMounted确保容器存在,再完成地图初始化,保证地图正常显示
  onMounted(() => {
      initMap();
  });

  // 地图实例化函数
  const initMap = () => {
  // const url = "http://localhost:8090/iserver/services/map-chengdu/rest/maps/chengdu_sheng%40chengdu";
  // const url = "http://localhost:8090/iserver/services/map-cd_data/rest/maps/chengdu_alldata";
  const url = "https://iserver.supermap.io/iserver/services/map-world/rest/maps/World"
  const map = new Map({
      layers: [
          new TileLayer({
              source: new TileSuperMapRest({url: url,wrapX: true,}),
              projection: "EPSG:4326",
        }),
      ],
      view: new View({
          center: [104.08 , 30.75],
          zoom: 2,
          projection:'EPSG:4326'
      }),
      target: 'maplayer',
      controls:[
        new ScaleLine({
          className:'ol-scale-line',
          target:document.getElementById('scale-bar'),
          minWidth:64,
          units:'metric'
      })
      ]
  });
  }
</script>

地图显示正常
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值