uniapp實現(xiàn)地圖軌跡回放

<template>
    <view class="page">
        <map v-if="polyline[0].points.length > 0" id="mymap" style="width: 100%;height: 80vh;"
            :longitude="polyline[0].points[0].longitude" 
            :latitude="polyline[0].points[0].latitude" 
            :markers="makers"
            :polyline="polyline"
            show-location
            show-compass
            enable-3D   
            :include-points="polyline[0].points"></map>
        <view class="">
            <button type="default" v-if="startMove" @click="handleStopMove()">暫停移動</button>
            <button type="default" v-else @click="handleStartMove()">開始移動</button>
            
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                id: 0, // 使用 marker點擊事件 需要填寫id
                mapContext: null,
                nextPointIndex: 1,      //下一個坐標(biāo)點的索引
                duratioinTime: 1000, ////相鄰兩點動畫持續(xù)時長默認(rèn)1秒
                makers: [{
                    id: 1,
                    width: 40,
                    height: 40,
                    latitude: 0,
                    longitude: 0,
                    iconPath: 'xxxxxxx,
                    anchor: {
                        x: 0.5,
                        y: 1
                    }
                }],
                polyline: [{
                    width: 8,
                    points: [],
                    arrowLine: true,
                    color: '#3591FC',
                }],
                startMove: false,//是否開啟回放
            }
        },
        onLoad() {
            this.getTrack()
            console.log(this.polyline[0].points)
            uni.getLocation({
                type: 'wgs84',
                success: function(res) {
                    console.log(res,'r')
                },
                fail: function(res) {
                    console.log(res,'fail')
                }
            })
        },
        methods: {
            //模擬獲取遠(yuǎn)程數(shù)據(jù)
            getTrack() {
                this.polyline[0].points = [{
                        latitude: 39.997761,
                        longitude: 116.478935
                    },
                    {
                        latitude: 39.997825,
                        longitude: 116.478939
                    },
                    {
                        latitude: 39.998549,
                        longitude: 116.478912
                    },
                    {
                        latitude: 39.998555,
                        longitude: 116.478998
                    },
                    {
                        latitude: 39.998566,
                        longitude: 116.479282
                    },
                    {
                        latitude: 39.998528,
                        longitude: 116.479658
                    },
                    {
                        latitude: 39.998453,
                        longitude: 116.480151
                    },
                    {
                        latitude: 39.998302,
                        longitude: 116.480784
                    },
                    {
                        latitude: 39.998184,
                        longitude: 116.481149
                    },
                    {
                        latitude: 39.997997,
                        longitude: 116.481573
                    },
                    {
                        latitude: 39.997846,
                        longitude: 116.481863
                    },
                    {
                        latitude: 39.997718,
                        longitude: 116.482072
                    },
                    {
                        latitude: 39.997718,
                        longitude: 116.482362
                    },
                    {
                        latitude: 39.998935,
                        longitude: 116.483633
                    },
                    {
                        latitude: 39.998968,
                        longitude: 116.48367
                    },
                    {
                        latitude: 39.999861,
                        longitude: 116.484648
                    }
                ]
                this.duratioinTime = Math.ceil(3000 / this.polyline[0].points.length) //默認(rèn)播放全程使用30秒雹食,計算相連兩點動畫時長
                this.initMapData()

            },
            //設(shè)置位置(從起點開始)
            initMakers() {
                this.makers[0].latitude = this.polyline[0].points[0].latitude;
                this.makers[0].longitude = this.polyline[0].points[0].longitude;
            },
            //設(shè)置地圖
            initMapData() {
                this.initMakers()
                this.mapContext = uni.createMapContext('mymap', this)
            },
            //移動坐標(biāo)
            movePoint() {
                this.mapContext.translateMarker({
                    duration: this.duratioinTime,
                    markerId:this.makers[0].id,
                    destination: {
                        latitude: this.polyline[0].points[this.nextPointIndex].latitude,
                        longitude: this.polyline[0].points[this.nextPointIndex].longitude
                    },
                    animationEnd: res => {
                        //播放結(jié)束贿肩,繼續(xù)移動到下一個點保檐,最后一個點時結(jié)束移動
                        if(this.nextPointIndex<this.polyline[0].points.length-1) {
                            this.nextPointIndex ++
                            if(this.startMove) {
                                this.movePoint()
                            }
                        }else {
                            this.nextPointIndex = 1
                            this.startMove = false
                        }

                    }
                })
            },
            //開始移動
            handleStartMove() {
                this.startMove = true
                this.movePoint()
            },
            //暫停移動
            handleStopMove() {
                this.startMove = false
            }
        },
        mounted() {

        }
    }
</script>

<style>

</style>

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末兜粘,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子回铛,更是在濱河造成了極大的恐慌星著,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剃浇,死亡現(xiàn)場離奇詭異,居然都是意外死亡猎物,警方通過查閱死者的電腦和手機虎囚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蔫磨,“玉大人淘讥,你說我怎么就攤上這事〉倘纾” “怎么了蒲列?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵窒朋,是天一觀的道長。 經(jīng)常有香客問我蝗岖,道長侥猩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任抵赢,我火速辦了婚禮拭宁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘瓣俯。我一直安慰自己,他們只是感情好兵怯,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布彩匕。 她就那樣靜靜地躺著,像睡著了一般媒区。 火紅的嫁衣襯著肌膚如雪驼仪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天袜漩,我揣著相機與錄音绪爸,去河邊找鬼。 笑死宙攻,一個胖子當(dāng)著我的面吹牛奠货,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播座掘,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼递惋,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了溢陪?” 一聲冷哼從身側(cè)響起萍虽,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎形真,沒想到半個月后杉编,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡咆霜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年邓馒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛾坯。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡绒净,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出偿衰,到底是詐尸還是另有隱情挂疆,我是刑警寧澤改览,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站缤言,受9級特大地震影響宝当,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜胆萧,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一庆揩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跌穗,春花似錦订晌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至羹唠,卻和暖如春奕枢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背佩微。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工缝彬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哺眯。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓谷浅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親奶卓。 傳聞我的和親對象是個殘疾皇子壳贪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容