vue中mapbox地圖顯示一半的問題及解決方法
解決vue中mapbox地圖顯示一半的問題
問題描述: 在vue中創(chuàng)建mapbox地圖,地圖只顯示一般,查看瀏覽器開發(fā)者工具。發(fā)現(xiàn)將canvas.mapboxgl-canvas 的position:absolute去掉就解決了 。
代碼修改:獲取到canvas.mapboxgl-canvas,并修改其position樣式就ok


修改前代碼:
修改后
添加
this.map.on("load", () => {
// Wait for map to load before modifying styles
const canvas = this.$refs.mapContainer.querySelector('.mapboxgl-canvas');
canvas.style.position = 'relative';
});完整代碼:
<template>
<main>
<p>Center :{{center}}</p>
<p>Zoom : {{ zoom }}</p>
<div id="map" class="map-container" ref="mapContainer">
</div>
</main>
</template>
<script>
import mapboxGl from "mapbox-gl";
export default {
name:"MapMapbox",
data(){
return {
center:[-93.1247, 44.9323],
zoom:10.5
}
},
mounted() {
mapboxGl.accessToken = "your_mapbox_token";
this.createMap();
console.log(this.map)
},
methods: {
createMap() {
this.map = new mapboxGl.Map({
container: "map",
style: "mapbox://styles/mapbox/streets-v9",
minzoom: 5,
center: this.center,
zoom: this.zoom,
hash: true
});
this.map.on("load", () => {
// Wait for map to load before modifying styles
const canvas = this.$refs.mapContainer.querySelector('.mapboxgl-canvas');
canvas.style.position = 'relative';
});
this.map.on("move", () => {
this.center = this.map.getCenter();
});
this.map.on("zoom", () => {
this.zoom = this.map.getZoom();
});
}
},
beforeDestroy() {
if (this.map) {
this.map.remove();
}
}
}
</script>
<style scoped>
.map-container {
height: 500px;
width: 100%;
}
</style>到此這篇關(guān)于解決vue中mapbox地圖顯示一半的問題的文章就介紹到這了,更多相關(guān)vue中mapbox地圖顯示一半內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue首屏加載出現(xiàn)白屏問題的優(yōu)化實戰(zhàn)
在實際開發(fā)中,Vue?應用首次加載時經(jīng)常出現(xiàn)白屏問題,這嚴重影響了用戶體驗,本文將從多個角度出發(fā),提供全面的解決方案,大家可以根據(jù)需要進行選擇2025-05-05
Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明
這篇文章主要介紹了Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
深入淺析Vue中mixin和extend的區(qū)別和使用場景
Vue中有兩個較為高級的靜態(tài)方法mixin和extend,接下來給大家介紹Vue中mixin和extend的區(qū)別和使用場景,感興趣的朋友一起看看吧2019-08-08
淺談vite和webpack的性能優(yōu)化和區(qū)別
本文主要介紹了淺談vite和webpack的區(qū)別,從性能優(yōu)化的幾個方便講解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

