vue項(xiàng)目使用高德地圖時(shí)報(bào)錯(cuò):AMap?is?not?defined解決辦法
場(chǎng)景:
最近在使用高德地圖做軌跡回放的時(shí)候,遇到一個(gè)BUG,提示:AMap is not defined,大概就是沒(méi)找到你定義的AMap
網(wǎng)上找了很久,比如什么js加載順序問(wèn)題
還有說(shuō)要設(shè)置

統(tǒng)統(tǒng)試了,都沒(méi)用?。?!
解決辦法:
既然說(shuō)加載順序的問(wèn)題,那就在初始化的時(shí)候,給加個(gè)延遲吧

結(jié)果發(fā)現(xiàn)好了!??!

我這種投機(jī)取巧的方式也許沒(méi)有徹底解決問(wèn)題,目前沒(méi)找到更好的辦法,希望各位大神指點(diǎn)指點(diǎn)
下面是整個(gè)頁(yè)面的完整代碼
<template>
<div>
<div id="container"></div>
<div class="input-card">
<h4>軌跡回放控制</h4>
<div class="input-item">
<input type="button" class="btn" value="開始動(dòng)畫" id="start" @click="startAnimation()" />
<input type="button" class="btn" value="暫停動(dòng)畫" id="pause" @click="pauseAnimation()" />
</div>
<div class="input-item">
<input type="button" class="btn" value="繼續(xù)動(dòng)畫" id="resume" @click="resumeAnimation()" />
<input type="button" class="btn" value="停止動(dòng)畫" 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>
//請(qǐng)求路徑
//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項(xiàng)目使用高德地圖時(shí)報(bào)錯(cuò):AMap is not defined解決辦法的文章就介紹到這了,更多相關(guān)vue高德地圖報(bào)錯(cuò)AMap is not defined內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 頁(yè)面回退mounted函數(shù)不執(zhí)行的解決方案
這篇文章主要介紹了vue 頁(yè)面回退mounted函數(shù)不執(zhí)行的解決方案 ,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue.js中created()與activated()的個(gè)人使用解讀
這篇文章主要介紹了vue.js中created()與activated()的個(gè)人使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能(拖動(dòng)過(guò)快失效問(wèn)題解決方法)
這篇文章主要介紹了vue+mousemove實(shí)現(xiàn)鼠標(biāo)拖動(dòng)功能,文中給大家介紹了鼠標(biāo)移動(dòng)過(guò)快拖動(dòng)就失效問(wèn)題的解決方法,需要的朋友可以參考下2018-08-08
vue router自動(dòng)判斷左右翻頁(yè)轉(zhuǎn)場(chǎng)動(dòng)畫效果
最近公司項(xiàng)目比較少終于有空來(lái)記錄一下自己對(duì)vue-router的一些小小的使用心得,本文給大家分享vue router自動(dòng)判斷左右翻頁(yè)轉(zhuǎn)場(chǎng)動(dòng)畫效果,感興趣的朋友一起看看吧2017-10-10
使用Vue3+ElementPlus前端實(shí)現(xiàn)分片上傳的全過(guò)程
ElementPlus是一套為開發(fā)者、設(shè)計(jì)師和產(chǎn)品經(jīng)理準(zhǔn)備的基于Vue?3.0的組件庫(kù),提供了配套設(shè)計(jì)資源,幫助你的網(wǎng)站快速成型,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+ElementPlus前端實(shí)現(xiàn)分片上傳的相關(guān)資料,需要的朋友可以參考下2022-11-11
詳解用vue.js和laravel實(shí)現(xiàn)微信支付
本篇文章主要介紹了用vue.js和laravel實(shí)現(xiàn)微信支付,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明
這篇文章主要介紹了vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue Components 數(shù)字鍵盤的實(shí)現(xiàn)
這篇文章主要介紹了Vue Components 數(shù)字鍵盤的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

