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="開(kāi)始動(dòng)畫(huà)" id="start" @click="startAnimation()" />
<input type="button" class="btn" value="暫停動(dòng)畫(huà)" id="pause" @click="pauseAnimation()" />
</div>
<div class="input-item">
<input type="button" class="btn" value="繼續(xù)動(dòng)畫(huà)" id="resume" @click="resumeAnimation()" />
<input type="button" class="btn" value="停止動(dòng)畫(huà)" 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-cli 中為單獨(dú)頁(yè)面設(shè)置背景圖片鋪滿(mǎn)全屏
這篇文章主要介紹了使用Vue-cli 中為單獨(dú)頁(yè)面設(shè)置背景圖片鋪滿(mǎn)全屏,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue實(shí)現(xiàn)動(dòng)態(tài)響應(yīng)數(shù)據(jù)變化
本篇文章主要介紹了Vue 動(dòng)態(tài)響應(yīng)數(shù)據(jù)變化,通過(guò)綁定數(shù)據(jù)即可以實(shí)時(shí)改變視圖顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Vue3 給 reactive 響應(yīng)式對(duì)象賦值的實(shí)現(xiàn)
在Vue3中,可以使用reactive函數(shù)創(chuàng)建響應(yīng)式對(duì)象,如果你想給這個(gè)響應(yīng)式對(duì)象賦值,可以直接修改其屬性,下面就來(lái)介紹一下reactive響應(yīng)式對(duì)象賦值的實(shí)現(xiàn),感興趣的可以了解一下2025-09-09
vue中使用element組件時(shí)事件想要傳遞其他參數(shù)的問(wèn)題
這篇文章主要介紹了vue中使用element組件時(shí)事件想要傳遞其他參數(shù)的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
vue項(xiàng)目本地開(kāi)發(fā)完成后部署到服務(wù)器后報(bào)404錯(cuò)誤解決方案
很多時(shí)候我們發(fā)現(xiàn)辛辛苦苦寫(xiě)的VueJs應(yīng)用經(jīng)過(guò)打包后在自己本地搭建的服務(wù)器上測(cè)試沒(méi)有什么問(wèn)題,但真正放在服務(wù)器上后會(huì)發(fā)現(xiàn)或多或少的問(wèn)題,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目本地開(kāi)發(fā)完成后部署到服務(wù)器后報(bào)404錯(cuò)誤的解決方案,需要的朋友可以參考下2024-01-01
Vue防止白屏添加首屏動(dòng)畫(huà)的實(shí)例
今天小編就為大家分享一篇Vue防止白屏添加首屏動(dòng)畫(huà)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

