VUE引入騰訊地圖并實(shí)現(xiàn)軌跡動(dòng)畫的詳細(xì)步驟
效果:

引入步驟:
- 在 html 中通過引入 script 標(biāo)簽加載API服務(wù)
- 在一個(gè)盒子元素 div 中預(yù)先準(zhǔn)備地圖容器,并在CSS樣式中定義地圖(容器)顯示大小
- 創(chuàng)建并顯示地圖的代碼
- 創(chuàng)建動(dòng)畫和標(biāo)記
1. 在 html 中通過引入 script 標(biāo)簽加載API服務(wù)
<script src="https://map.qq.com/api/gljs?v=1.exp&key=你申請(qǐng)的騰訊地圖應(yīng)用key"></script>
2. 在body中預(yù)先準(zhǔn)備地圖容器,并在CSS樣式中定義地圖(容器)顯示大小
<div id="container"></div>
#container {
width: 100%;
height: calc(100vh - 280px);
border-radius: 10px;
overflow: hidden;
}3. 創(chuàng)建并顯示地圖的代碼
mounted() {
this.initMap()
},
methods: {
initMap() {
//設(shè)置地圖中心點(diǎn)
let center = new TMap.LatLng(39.984104, 116.307503);
// 初始化地圖
this.map = new TMap.Map('container', {
zoom: 15,
center: center,
// baseMap: { // 設(shè)置衛(wèi)星地圖
// type: 'satellite'
// }
});
this.polylineLayer = new TMap.MultiPolyline({
map:this.map, // 繪制到目標(biāo)地圖
// 折線樣式定義
styles: {
style_blue: new TMap.PolylineStyle({
color: '#ff8d00', // 線填充色
width: 4, // 折線寬度
borderWidth: 2, // 邊線寬度
borderColor: '#FFF', // 邊線顏色
lineCap: 'round', // 線端頭方式
eraseColor: 'rgb(172,172,172)',//擦除路徑的顏色
}),
},
geometries: [
{
id: 'erasePath',
styleId: 'style_blue',
paths: this.path,
},
],
});
this.marker = new TMap.MultiMarker({
map:this.map, // 繪制到目標(biāo)地圖
styles: {
'car-down': new TMap.MarkerStyle({
width: 40,
height: 40,
anchor: {
x: 20,
y: 20,
},
faceTo: 'map',
rotate: 180,
src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/car.png',
}),
start: new TMap.MarkerStyle({
width: 25,
height: 35,
anchor: {x: 16, y: 32},
src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/start.png',
}),
end: new TMap.MarkerStyle({
width: 25,
height: 35,
anchor: {x: 16, y: 32},
src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/end.png',
}),
},
geometries: [
{
id: 'car',
styleId: 'car-down',
position: new TMap.LatLng(39.98481500648338, 116.30571126937866),
},
{
id: 'start',
styleId: 'start',
position: new TMap.LatLng(39.98481500648338, 116.30571126937866),
},
{
id: 'end',
styleId: 'end',
position: new TMap.LatLng(39.978813710266024, 116.31699800491333),
},
],
});
}
}4. 創(chuàng)建動(dòng)畫和標(biāo)記
?// 暫停動(dòng)畫
? ? ?pauseMove() {
? ? ? ? this.marker.pauseMove()
? ? ? },
? ? ?// 停止動(dòng)畫
? ? ? resumeMove() {
? ? ? ? this.marker.resumeMove()
? ? ? },
? ? ?// 開始動(dòng)畫
? ? ? startCar() {
? ? ? ? // 使用marker 移動(dòng)接口, https://lbs.qq.com/webApi/javascriptGL/glDoc/glDocMarker
? ? ? ? //調(diào)用moveAlong動(dòng)畫 執(zhí)行標(biāo)記點(diǎn)動(dòng)畫開始
? ? ? ? this.marker.moveAlong(
? ? ? ? ? {
? ? ? ? ? ? car: {
? ? ? ? ? ? ? path: this.path,//移動(dòng)路徑的坐標(biāo)串
? ? ? ? ? ? ? // duration:8000,//完成移動(dòng)所需的時(shí)間,單位:毫秒
? ? ? ? ? ? ? speed: 250, //speed為指定速度,正整數(shù),單位:千米/小時(shí)
? ? ? ? ? ? },
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? autoRotation: true,//自動(dòng)旋轉(zhuǎn)
? ? ? ? ? }
? ? ? ? );
? ? ? ? //監(jiān)聽事件名
? ? ? ? this.marker.on('moving', (e) => {
? ? ? ? ? var passedLatLngs = e.car && e.car.passedLatLngs;
? ? ? ? ? if (passedLatLngs) {
? ? ? ? ? ? // 使用路線擦除接口 eraseTo, https://lbs.qq.com/webApi/javascriptGL/glDoc/glDocVector
? ? ? ? ? ? this.polylineLayer.eraseTo(
? ? ? ? ? ? ? 'erasePath',
? ? ? ? ? ? ? passedLatLngs.length - 1,
? ? ? ? ? ? ? passedLatLngs[passedLatLngs.length - 1]
? ? ? ? ? ? );
? ? ? ? ? }
? ? ? ? });
?
? ? ? },地圖組件完整代碼
<template>
? <section>
? ? <el-button style="margin: 15px" size="mini" type="danger" @click="startCar">開始</el-button>
? ? <el-button style="margin: 15px" size="mini" type="danger" @click="pauseMove">暫停</el-button>
? ? <el-button style="margin: 15px" size="mini" type="info" @click="resumeMove">繼續(xù)</el-button>
? ? <div id="container"></div>
? </section>
</template>
?
<script>
? export default {
? ? name: "mk-map",
? ? data() {
? ? ? return {
? ? ? ? map: null,
? ? ? ? path: [
? ? ? ? ? new TMap.LatLng(39.98481500648338, 116.30571126937866),
? ? ? ? ? new TMap.LatLng(39.982266575222155, 116.30596876144409),
? ? ? ? ? new TMap.LatLng(39.982348784165886, 116.3111400604248),
? ? ? ? ? new TMap.LatLng(39.978813710266024, 116.3111400604248),
? ? ? ? ? new TMap.LatLng(39.978813710266024, 116.31699800491333),
? ? ? ? ? new TMap.LatLng(39.988813710266024, 116.31699800491333),
? ? ? ? ? new TMap.LatLng(39.989813710266024, 116.31699800491333),
? ? ? ? ? new TMap.LatLng(39.990813710266024, 116.31699800491333),
? ? ? ? ? new TMap.LatLng(39.98481500648338, 116.30571126937866),
? ? ? ? ],
? ? ? ? polylineLayer: null,
? ? ? ? marker: null
? ? ? }
? ? },
? ? mounted() {
? ? ? this.initMap()
? ? },
? ? methods: {
? ? ? pauseMove() {
? ? ? ? this.marker.pauseMove()
? ? ? },
? ? ? resumeMove() {
? ? ? ? this.marker.resumeMove()
? ? ? },
? ? ? startCar() {
? ? ? ? // 使用marker 移動(dòng)接口, https://lbs.qq.com/webApi/javascriptGL/glDoc/glDocMarker
? ? ? ? //調(diào)用moveAlong動(dòng)畫 執(zhí)行標(biāo)記點(diǎn)動(dòng)畫開始
? ? ? ? this.marker.moveAlong(
? ? ? ? ? {
? ? ? ? ? ? car: {
? ? ? ? ? ? ? path: this.path,//移動(dòng)路徑的坐標(biāo)串
? ? ? ? ? ? ? // duration:8000,//完成移動(dòng)所需的時(shí)間,單位:毫秒
? ? ? ? ? ? ? speed: 250, //speed為指定速度,正整數(shù),單位:千米/小時(shí)
? ? ? ? ? ? },
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? autoRotation: true,//自動(dòng)旋轉(zhuǎn)
? ? ? ? ? }
? ? ? ? );
? ? ? ? //監(jiān)聽事件名
? ? ? ? this.marker.on('moving', (e) => {
? ? ? ? ? var passedLatLngs = e.car && e.car.passedLatLngs;
? ? ? ? ? if (passedLatLngs) {
? ? ? ? ? ? // 使用路線擦除接口 eraseTo, https://lbs.qq.com/webApi/javascriptGL/glDoc/glDocVector
? ? ? ? ? ? this.polylineLayer.eraseTo(
? ? ? ? ? ? ? 'erasePath',
? ? ? ? ? ? ? passedLatLngs.length - 1,
? ? ? ? ? ? ? passedLatLngs[passedLatLngs.length - 1]
? ? ? ? ? ? );
? ? ? ? ? }
? ? ? ? });
?
? ? ? },
? ? ? initMap() {
? ? ? ? //設(shè)置地圖中心點(diǎn)
? ? ? ? let center = new TMap.LatLng(39.984104, 116.307503);
? ? ? ? // 初始化地圖
? ? ? ? this.map = new TMap.Map('container', {
? ? ? ? ? zoom: 15,
? ? ? ? ? center: center,
? ? ? ? ? // baseMap: { ?// 設(shè)置衛(wèi)星地圖
? ? ? ? ? // ? type: 'satellite'
? ? ? ? ? // }
? ? ? ? });
? ? ? ? this.polylineLayer = new TMap.MultiPolyline({
? ? ? ? ? map:this.map, // 繪制到目標(biāo)地圖
? ? ? ? ? // 折線樣式定義
? ? ? ? ? styles: {
? ? ? ? ? ? style_blue: new TMap.PolylineStyle({
? ? ? ? ? ? ? color: '#ff8d00', // 線填充色
? ? ? ? ? ? ? width: 4, // 折線寬度
? ? ? ? ? ? ? borderWidth: 2, // 邊線寬度
? ? ? ? ? ? ? borderColor: '#FFF', // 邊線顏色
? ? ? ? ? ? ? lineCap: 'round', // 線端頭方式
? ? ? ? ? ? ? eraseColor: 'rgb(172,172,172)',//擦除路徑的顏色
? ? ? ? ? ? }),
? ? ? ? ? },
? ? ? ? ? geometries: [
? ? ? ? ? ? {
? ? ? ? ? ? ? id: 'erasePath',
? ? ? ? ? ? ? styleId: 'style_blue',
? ? ? ? ? ? ? paths: this.path,
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? });
? ? ? ? this.marker = new TMap.MultiMarker({
? ? ? ? ? map:this.map, // 繪制到目標(biāo)地圖
? ? ? ? ? styles: {
? ? ? ? ? ? 'car-down': new TMap.MarkerStyle({
? ? ? ? ? ? ? width: 40,
? ? ? ? ? ? ? height: 40,
? ? ? ? ? ? ? anchor: {
? ? ? ? ? ? ? ? x: 20,
? ? ? ? ? ? ? ? y: 20,
? ? ? ? ? ? ? },
? ? ? ? ? ? ? faceTo: 'map',
? ? ? ? ? ? ? rotate: 180,
? ? ? ? ? ? ? src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/car.png',
? ? ? ? ? ? }),
? ? ? ? ? ? start: new TMap.MarkerStyle({
? ? ? ? ? ? ? width: 25,
? ? ? ? ? ? ? height: 35,
? ? ? ? ? ? ? anchor: {x: 16, y: 32},
? ? ? ? ? ? ? src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/start.png',
? ? ? ? ? ? }),
? ? ? ? ? ? end: new TMap.MarkerStyle({
? ? ? ? ? ? ? width: 25,
? ? ? ? ? ? ? height: 35,
? ? ? ? ? ? ? anchor: {x: 16, y: 32},
? ? ? ? ? ? ? src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/end.png',
? ? ? ? ? ? }),
? ? ? ? ? },
? ? ? ? ? geometries: [
? ? ? ? ? ? {
? ? ? ? ? ? ? id: 'car',
? ? ? ? ? ? ? styleId: 'car-down',
? ? ? ? ? ? ? position: new TMap.LatLng(39.98481500648338, 116.30571126937866),
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? id: 'start',
? ? ? ? ? ? ? styleId: 'start',
? ? ? ? ? ? ? position: new TMap.LatLng(39.98481500648338, 116.30571126937866),
? ? ? ? ? ? },
? ? ? ? ? ? {
? ? ? ? ? ? ? id: 'end',
? ? ? ? ? ? ? styleId: 'end',
? ? ? ? ? ? ? position: new TMap.LatLng(39.978813710266024, 116.31699800491333),
? ? ? ? ? ? },
? ? ? ? ? ],
? ? ? ? });
? ? ? }
? ? }
? }
</script>
?
<style lang="scss" scoped>
? #container {
? ? width: 100%;
? ? height: calc(100vh - 280px);
? ? border-radius: 10px;
? ? overflow: hidden;
? }
</style>到此這篇關(guān)于VUE引入騰訊地圖并實(shí)現(xiàn)軌跡動(dòng)畫的文章就介紹到這了,更多相關(guān)vue引入騰訊地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決報(bào)錯(cuò)ValidationError: Progress Plugin Invalid&
這篇文章主要介紹了解決報(bào)錯(cuò)ValidationError: Progress Plugin Invalid Options問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue 實(shí)現(xiàn)CLI 3.0 + momentjs + lodash打包時(shí)優(yōu)化
今天小編就為大家分享一篇Vue 實(shí)現(xiàn)CLI 3.0 + momentjs + lodash打包時(shí)優(yōu)化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
element中TimePicker時(shí)間選擇器禁用部分時(shí)間(顯示禁用到分鐘)
這篇文章主要介紹了element中TimePicker時(shí)間選擇器禁用部分時(shí)間(顯示禁用到分鐘),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
element step組件在另一側(cè)加時(shí)間軸顯示
本文主要介紹了element step組件在另一側(cè)加時(shí)間軸顯示,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
vue3中項(xiàng)目?jī)?yōu)化方法詳解(Web?Worker的使用)
最近在做vue3的項(xiàng)目中,遇到了計(jì)算量龐大導(dǎo)致頁面響應(yīng)緩慢的問題,所以下面這篇文章主要給大家介紹了關(guān)于vue3中項(xiàng)目?jī)?yōu)化方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
vue實(shí)現(xiàn)點(diǎn)擊按鈕下載文件功能
這篇文章主要介紹了vue中點(diǎn)擊按鈕下載文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問題
這篇文章主要介紹了Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

