前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的步驟
預(yù)覽效果:

一、獲取高德地圖API的key(相當(dāng)于獲取開發(fā)許可權(quán),沒有就用不了)
注冊高德賬號(hào),注冊成功后復(fù)制 Key 值到組件,就可以使用。

二、安裝依賴
cnpm install @amap/amap-jsapi-loade
三、頁面代碼
<template>
<div class="h-md flex justify-center">
<topBar></topBar>
<div class="h-[600px] w-[1000px] mt-40" id="container"></div>
</div>
<div
class="fixed right-[30px] bottom-[30px] p-4 rounded-3xl border shadow-md border-blue-400 shadow-blue-700"
>
<h4>軌跡回放控制</h4>
<div class="flex mb-1">
<a-button class="mr-1" @click="startAnimation">開始動(dòng)畫</a-button>
<a-button @click="pauseAnimation">暫停動(dòng)畫</a-button>
</div>
<div class="flex">
<a-button class="mr-1" @click="resumeAnimation">繼續(xù)動(dòng)畫</a-button>
<a-button @click="stopAnimation">停止動(dòng)畫</a-button>
</div>
</div>
</template>
<script setup>
import { onMounted, onUnmounted, ref } from 'vue'
import topBar from '../shopCenter/topBar.vue'
import AMapLoader from '@amap/amap-jsapi-loader'
let map = null
const marker = ref()
const lineArr = ref([
[116.478935, 39.997761],
[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]
])
//開始動(dòng)畫
const startAnimation = () => {
marker.value.moveAlong(lineArr.value, {
duration: 500,
autoRotation: true
})
}
//暫停動(dòng)畫
const pauseAnimation = () => {
marker.value.pauseMove()
}
//繼續(xù)動(dòng)畫
const resumeAnimation = () => {
marker.value.resumeMove()
}
//停止動(dòng)畫
const stopAnimation = () => {
marker.value.stopMove()
}
onMounted(() => {
AMapLoader.load({
key: '111111111111111111111111', // 申請(qǐng)好的Web端開發(fā)者Key,首次調(diào)用 load 時(shí)必填
version: '2.0', // 指定要加載的 JSAPI 的版本,缺省時(shí)默認(rèn)為 1.4.15
plugins: [] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
})
.then((AMap) => {
// JSAPI2.0 使用覆蓋物動(dòng)畫必須先加載動(dòng)畫插件
AMap.plugin('AMap.MoveAnimation', function () {
map = new AMap.Map('container', {
// 設(shè)置地圖容器id
viewMode: '3D', // 是否為3D地圖模式
zoom: 17, // 初始化地圖級(jí)別
resizeEnable: true,
// center: [116.397428, 39.90923] // 初始化地圖中心點(diǎn)位置
center: [116.478935, 39.997761] // 初始化地圖中心點(diǎn)位置
})
//小車配置
marker.value = new AMap.Marker({
map: map,
position: [116.478935, 39.997761],
icon: 'https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png',
offset: new AMap.Pixel(-13, -26)
})
// 繪制軌跡
var polyline = new AMap.Polyline({
map: map,
path: lineArr.value,
showDir: true,
strokeColor: '#28F', //線顏色
// strokeOpacity: 1, //線透明度
strokeWeight: 6 //線寬
// strokeStyle: "solid" //線樣式
})
//移動(dòng)后的軌跡
var passedPolyline = new AMap.Polyline({
map: map,
strokeColor: '#AF5', //線顏色
strokeWeight: 6 //線寬
})
marker.value.on('moving', function (e) {
console.log('!!')
passedPolyline.setPath(e.passedPath)
map.setCenter(e.target.getPosition(), true)
})
map.setFitView()
})
})
.catch((e) => {
console.log(e)
})
})
onUnmounted(() => {
map?.destroy()
})
</script>
<style scoped></style>
官網(wǎng)示例:軌跡回放-點(diǎn)標(biāo)記-示例中心-JS API 2.0 示例 | 高德地圖API (amap.com)
總結(jié)
到此這篇關(guān)于前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的文章就介紹到這了,更多相關(guān)Vue3引入高德地圖展示行駛軌跡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
watch(監(jiān)視屬性)和computed(計(jì)算屬性)的區(qū)別及實(shí)現(xiàn)案例
watch和computed是vue實(shí)例對(duì)象中的兩個(gè)重要屬性,watch是監(jiān)視屬性,用來監(jiān)視vue實(shí)例對(duì)象上屬性和方法的變化,computed被稱為計(jì)算屬性,可以將data對(duì)象中的屬性進(jìn)行計(jì)算得到新的屬性,這篇文章主要介紹了watch(監(jiān)視屬性)和computed(計(jì)算屬性)的區(qū)別,需要的朋友可以參考下2023-05-05
vue 在methods中調(diào)用mounted的實(shí)現(xiàn)操作
這篇文章主要介紹了vue 在methods中調(diào)用mounted的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue 實(shí)現(xiàn)監(jiān)聽窗口關(guān)閉事件,并在窗口關(guān)閉前發(fā)送請(qǐng)求
vue如何解決sass-loader的版本過高導(dǎo)致的編譯錯(cuò)誤
vue cli3 實(shí)現(xiàn)分環(huán)境打包的步驟
Vue3中watchEffect高級(jí)偵聽器的具體使用

