如果首先隐藏容器,如何在Vue2Leaflet中调整地图大小

时间:2019-05-12 20:16:38

标签: vuejs2 bootstrap-4 leaflet resize maps

如果Vue2Leaflet首先被隐藏,然后又被显示(例如,如果被bootstrap折叠元素隐藏了),如何从this issue重新加载Leaflet地图?

<template>
  <b-container>
    <b-button v-b-toggle="collapse" variant="primary">Show map</b-button>
    <b-collapse id="collapse">
      <div id="map">
        <LMap :zoom=13 :center="[47.413220, -1.219482]">
          <LTileLayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"></LTileLayer>
        </LMap>
      </div>
    </b-collapse>
  </b-container>
</template>

<script>
import Vue from 'vue'
import { LMap, LTileLayer } from 'vue2-leaflet'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import 'leaflet/dist/leaflet.css'

Vue.use(BootstrapVue)
...
export default {
  ...
  components: {
    LMap,
    LTileLayer
  }
}
</script>

<style>
div#map {
  height: 300px;
  width: 500px;
}
</style>

1 个答案:

答案 0 :(得分:0)

我通过使用来自地图对象的invalidateSize函数(如GitHub issue的建议)并使用折叠元素的@shown属性来使其工作:

<template>
  <b-container>
    <b-button v-b-toggle="collapse" variant="primary">Show map</b-button>
    <b-collapse @shown="reloadMap()" id="collapse">
      <div id="map">
        <LMap ref="map" ...>
          ...
        </LMap>
      </div>
    </b-collapse>
  </b-container>
</template>

<script>
...
export default {
  ...
  methods: {
    reloadMap: function () {
      this.$refs.map.mapObject.invalidateSize()
    }
  }
}
</script>