完美解決vue引入BMapGL is not defined的問題
更新時間:2024年10月18日 09:33:23 作者:邱志剛
在Vue項目中使用BMapGL時,通過在src下添加bmp.js文件并配置密鑰(ak),可以有效解決地圖API的應用問題,本方法是基于個人經驗總結,希望能為開發(fā)者提供參考和幫助
vue引入BMapGL is not defined

在項目src下添加bmp.js
內容如下:(ak是密鑰)
// bmp.js
export function BMPGL(ak) {
return new Promise(function(resolve, reject) {
window.init = function() {
// eslint-disable-next-line
resolve(BMapGL)
}
const script = document.createElement('script')
script.type = 'text/javascript'
script.src = `http://api.map.baidu.com/api?v=1.0&type=webgl&ak=${ak}&callback=init`
script.onerror = reject
document.head.appendChild(script)
})
}在要用到BMapGL的vue文件中導入
<template>
<div class="home">
<!--創(chuàng)建地圖容器-->
<div id="admap" class="allmap"></div>
</div>
</template>
<script>
import { BMPGL } from "@/bmpgl.js"
export default {
name: "home",
data() {
return {
ak: "XXXXXXXXX", // 百度的地圖密鑰
myMap: null
};
},
mounted() {
this.initMap()
},
methods: {
initMap() {
// 傳入密鑰獲取地圖回調。
BMPGL(this.ak).then((BMapGL) => {
// 創(chuàng)建地圖實例
let map = new BMapGL.Map("admap");
// 創(chuàng)建點坐標 axios => res 獲取的初始化定位坐標
let point = new BMapGL.Point(114.031761, 22.542826)
// 初始化地圖,設置中心點坐標和地圖級別
map.centerAndZoom(point, 19)
//開啟鼠標滾輪縮放
map.enableScrollWheelZoom(true)
map.setHeading(64.5)
//map.setTilt(73)
// 保存數(shù)據(jù)
// this.myMap = map
})
.catch((err)=>{
console.log(err)
})
},
}
};
</script>
<style lang="scss" scoped>
.admap{
width: 100%;
height: 100vh;
position: relative;
z-index: 1;
}
</style>完美解決啦!
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue項目引入百度地圖BMapGL鼠標繪制和BMap輔助工具
- 解決Vue使用百度地圖BMapGL內存泄漏問題?Out?of?Memory
- vue中報錯“error‘xxx‘?is?defined?but?never?used”問題及解決
- Vue-cli3 $ is not defined錯誤問題及解決
- 解決Vue控制臺報錯Failed to mount component: template or render function not defined.
- vue報錯Cannot?read?properties?of?undefined?(...)類型的解決辦法
- vue在data中定義變量后依舊報undefined的解決
相關文章
解決vue項目使用font-awesome,build后路徑的問題
今天小編就為大家分享一篇解決vue項目使用font-awesome,build后路徑的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
element-plus 在vue3 中不生效的原因解決方法(element-plus引入)
這篇文章主要介紹了element-plus 在vue3 中不生效的原因解決方法(element-plus引入),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
vue+springboot+element+vue-resource實現(xiàn)文件上傳教程
這篇文章主要介紹了vue+springboot+element+vue-resource實現(xiàn)文件上傳教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

