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

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

二、安裝依賴
cnpm install @amap/amap-jsapi-loade
三、頁(yè)面代碼
<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)文章
VUE?項(xiàng)目如何使用?Docker+Nginx進(jìn)行打包部署
使用?Docker,你可以創(chuàng)建一個(gè)包含?Vue.js?應(yīng)用程序的容器鏡像,并在任何支持?Docker?的環(huán)境中運(yùn)行該鏡像,這篇文章主要介紹了VUE?項(xiàng)目用?Docker+Nginx進(jìn)行打包部署,需要的朋友可以參考下2024-06-06
Vue點(diǎn)擊在彈窗外部實(shí)現(xiàn)一鍵關(guān)閉的示例代碼
在Vue應(yīng)用中,彈窗是一個(gè)常見的交互元素,有時(shí)我們可能希望用戶點(diǎn)擊彈窗外部時(shí),彈窗能夠自動(dòng)關(guān)閉,本文主要介紹了Vue點(diǎn)擊在彈窗外部實(shí)現(xiàn)一鍵關(guān)閉的示例代碼,感興趣的可以了解一下2024-06-06
Vue實(shí)現(xiàn)模糊查詢filter()實(shí)例詳解
因?yàn)榻赵趯W(xué)習(xí)并使用VUE,客戶有一個(gè)要求,要輸入框可模糊查詢并帶有下拉提示的應(yīng)用,數(shù)據(jù)從接口取,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)模糊查詢filter()的相關(guān)資料,需要的朋友可以參考下2023-04-04
Vue導(dǎo)出excel的兩個(gè)常用方式介紹與對(duì)比
這篇文章主要為大家詳細(xì)介紹了Vue導(dǎo)出excel的兩個(gè)常用方式,分別為前端vue+XLSX導(dǎo)出excel和vue+后端POI?導(dǎo)出excel,感興趣的小伙伴可以了解下2025-01-01
Vue登錄主頁(yè)動(dòng)態(tài)背景短視頻制作
這篇文章主要為大家詳細(xì)介紹了Vue登錄主頁(yè)動(dòng)態(tài)背景短視頻的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
vue3通過(guò)父子傳值實(shí)現(xiàn)彈框功能
在Vue3中,我們可以通過(guò)?provide?和?inject?來(lái)實(shí)現(xiàn)父子組件之間的數(shù)據(jù)傳遞,這也適用于實(shí)現(xiàn)彈框功能,下面我們就來(lái)學(xué)習(xí)一下vue3實(shí)現(xiàn)彈框功能的具體方法吧2023-12-12
vue獲取DOM元素并設(shè)置屬性的兩種實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇vue獲取DOM元素并設(shè)置屬性的兩種實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

