vue項目使用高德地圖時報錯:AMap?is?not?defined解決辦法
場景:
最近在使用高德地圖做軌跡回放的時候,遇到一個BUG,提示:AMap is not defined,大概就是沒找到你定義的AMap
網(wǎng)上找了很久,比如什么js加載順序問題
還有說要設(shè)置
統(tǒng)統(tǒng)試了,都沒用?。?!
解決辦法:
既然說加載順序的問題,那就在初始化的時候,給加個延遲吧
結(jié)果發(fā)現(xiàn)好了!?。?/p>
我這種投機取巧的方式也許沒有徹底解決問題,目前沒找到更好的辦法,希望各位大神指點指點
下面是整個頁面的完整代碼
<template> <div> <div id="container"></div> <div class="input-card"> <h4>軌跡回放控制</h4> <div class="input-item"> <input type="button" class="btn" value="開始動畫" id="start" @click="startAnimation()" /> <input type="button" class="btn" value="暫停動畫" id="pause" @click="pauseAnimation()" /> </div> <div class="input-item"> <input type="button" class="btn" value="繼續(xù)動畫" id="resume" @click="resumeAnimation()" /> <input type="button" class="btn" value="停止動畫" id="stop" @click="stopAnimation()" /> </div> </div> </div> </template> <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=你自己的key"></script> <script> //請求路徑 //import { //playbacklist, //} from "@/api/obd/playback"; export default { mounted() { this.initMap(); }, beforeDestroy() { if (this.timer) { clearInterval(this.timer); this.timer = null; this.map && this.map.destroy(); } }, data() { return { map: null, marker: null, lineArr: [ [118.478939, 39.997768], [116.478939, 39.997825], [116.478912, 39.998549], [116.478912, 39.998549], [116.478998, 39.998555], [116.478998, 39.998555], [116.479282, 39.99856], [116.479658, 39.998528], [116.480151, 39.998453], [116.480784, 39.998302], [116.480784, 39.998302], [116.481149, 39.998184], [116.481573, 39.997997], [116.481863, 39.997846], [116.482072, 39.997718], [116.482362, 39.997718], [116.483633, 39.998935], [116.48367, 39.998968], [116.484648, 39.999861] ] }; }, methods: { initMap() { setTimeout(()=>{ var that = this that.map = new AMap.Map("container", { resizeEnable: true, center: [108.478939, 39.997768], zoom: 17 }); that.marker = new AMap.Marker({ map: that.map, position: [118.478939, 39.997768], icon: "https://webapi.amap.com/images/car.png", offset: new AMap.Pixel(-26, -15), autoRotation: true, angle: -90 }); // 繪制軌跡 var polyline = new AMap.Polyline({ map: that.map, path: that.lineArr, showDir: true, strokeColor: "#28F", //線顏色 // strokeOpacity: 1, //線透明度 strokeWeight: 6 //線寬 // strokeStyle: "solid" //線樣式 }); var passedPolyline = new AMap.Polyline({ map: that.map, // path: this.lineArr, strokeColor: "#AF5", //線顏色 // strokeOpacity: 1, //線透明度 strokeWeight: 6 //線寬 // strokeStyle: "solid" //線樣式 }); this.marker.on("moving", function (e) { passedPolyline.setPath(e.passedPath); }); this.map.setFitView(); },800) }, startAnimation() { this.marker.moveAlong(this.lineArr, 200); }, pauseAnimation() { this.marker.pauseMove(); }, resumeAnimation() { this.marker.resumeMove(); }, stopAnimation() { this.marker.stopMove(); } } }; </script> <style scoped> #container { height: 1000px; width: 100%; } .input-card .btn { margin-right: 1.2rem; width: 9rem; } .input-card .btn:last-child { margin-right: 0; } .btn { display: inline-block; font-weight: 400; text-align: center; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; border: 1px solid transparent; transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out; background-color: transparent; background-image: none; color: #25A5F7; border-color: #25A5F7; padding: .25rem .5rem; line-height: 1.5; border-radius: 1rem; cursor:pointer; } .input-item { position: relative; display: -ms-flexbox; display: flex; -ms-flex-wrap: wrap; flex-wrap: wrap; -ms-flex-align: center; align-items: center; width: 100%; height: 3rem; } .input-card { display: flex; flex-direction: column; min-width: 0; word-wrap: break-word; background-color: #fff; background-clip: border-box; border-radius: .25rem; width: 22rem; border-width: 0; border-radius: 0.4rem; box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5); position: fixed; bottom: 1rem; right: 1rem; -ms-flex: 1 1 auto; flex: 1 1 auto; padding: 0.75rem 1.25rem; } </style>
總結(jié)
到此這篇關(guān)于vue項目使用高德地圖時報錯:AMap is not defined解決辦法的文章就介紹到這了,更多相關(guān)vue高德地圖報錯AMap is not defined內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 頁面回退mounted函數(shù)不執(zhí)行的解決方案
這篇文章主要介紹了vue 頁面回退mounted函數(shù)不執(zhí)行的解決方案 ,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue.js中created()與activated()的個人使用解讀
這篇文章主要介紹了vue.js中created()與activated()的個人使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07vue+mousemove實現(xiàn)鼠標拖動功能(拖動過快失效問題解決方法)
這篇文章主要介紹了vue+mousemove實現(xiàn)鼠標拖動功能,文中給大家介紹了鼠標移動過快拖動就失效問題的解決方法,需要的朋友可以參考下2018-08-08vue router自動判斷左右翻頁轉(zhuǎn)場動畫效果
最近公司項目比較少終于有空來記錄一下自己對vue-router的一些小小的使用心得,本文給大家分享vue router自動判斷左右翻頁轉(zhuǎn)場動畫效果,感興趣的朋友一起看看吧2017-10-10使用Vue3+ElementPlus前端實現(xiàn)分片上傳的全過程
ElementPlus是一套為開發(fā)者、設(shè)計師和產(chǎn)品經(jīng)理準備的基于Vue?3.0的組件庫,提供了配套設(shè)計資源,幫助你的網(wǎng)站快速成型,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+ElementPlus前端實現(xiàn)分片上傳的相關(guān)資料,需要的朋友可以參考下2022-11-11vue項目input標簽checkbox,change和click綁定事件的區(qū)別說明
這篇文章主要介紹了vue項目input標簽checkbox,change和click綁定事件的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue Components 數(shù)字鍵盤的實現(xiàn)
這篇文章主要介紹了Vue Components 數(shù)字鍵盤的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09