Openlayers 實(shí)例-軌跡回放

幾年前用高德地圖做過手機(jī)版的軌跡回放妓湘,準(zhǔn)備用Openlayers來實(shí)現(xiàn)一個(gè)web版的軌跡回放联逻,軌跡回放的原理相對比較簡單甜刻,首先將車輛的軌跡添加在地圖上,然后再添加一個(gè)軌跡點(diǎn)纱扭,設(shè)置一個(gè)速度胰舆,讓軌跡點(diǎn)在線上移動塞蹭。實(shí)現(xiàn)方式如下所示:

添加軌跡

由于沒有數(shù)據(jù)棘捣,在地圖上隨便找了一些點(diǎn)用于測試。

//添加軌跡線和起始點(diǎn)
    addLineWithPoints(coordinates){
        var geom = new ol.geom.LineString(coordinates);
        //創(chuàng)建軌跡Feature
        var feature = new ol.Feature({
            geometry:geom
        })
        //創(chuàng)建矢量圖層
        var vectorLayer = LayerUtil.vectorLayer()
        //添加軌跡
        vectorLayer.getSource().addFeature(feature)
        //設(shè)置樣式
        vectorLayer.setStyle(this.getStyle())
        this.map.addLayer(vectorLayer)
        
        //添加起點(diǎn)
        var startFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[0])
        })
        startFeature.setStyle(new ol.style.Style({
            text:new ol.style.Text({
                font:'15px sans-serif',
                text:'起點(diǎn)',
                offsetY:-10
            })
        }))
        vectorLayer.getSource().addFeature(startFeature)
        //添加終點(diǎn)
        var endFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[coordinates.length - 1])
        })
        endFeature.setStyle(new ol.style.Style({
            text:new ol.style.Text({
                font:'15px sans-serif',
                text:'終點(diǎn)',
                offsetY:-10
            })
        }))
        vectorLayer.getSource().addFeature(endFeature)

        //添加小車默勾,這里使用一個(gè)圓點(diǎn)代替
        var carFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[0])
        })
        vectorLayer.getSource().addFeature(carFeature)
    }

添加的后效果如下所示:


軌跡.png

軌跡回放

添加好軌跡后碉渡,就要實(shí)現(xiàn)軌跡回放了,直接看代碼吧母剥,代碼中有注釋看起來也方便爆价,所有的代碼都在下邊:

class TrackPlayback{
    constructor(map){
        this.map = map
        this.animating = false//是否結(jié)束
        this.speed = 1//默認(rèn)速度
        this.now//當(dāng)前時(shí)間
    }

    getStyle(){
        var style = new ol.style.Style({
            fill: new ol.style.Fill({//填充樣式
              color: 'rgba(255, 255, 255, 0.2)'
            }),
            stroke: new ol.style.Stroke({//邊框樣式
              color: '#ffcc33',
              width: 2,
              lineCap:'square'
            }),
            image: new ol.style.Circle({//點(diǎn)樣式使用一個(gè)圓
              radius: 7,
              fill: new ol.style.Fill({
                color: '#ffcc33'
              })
            })
          });
        return style;
    }
    //添加軌跡線和起始點(diǎn)
    addLineWithPoints(coordinates){
        this.routeLength = coordinates.length
        this.routeCoords = coordinates
        var geom = new ol.geom.LineString(coordinates);
        //創(chuàng)建軌跡Feature
        var feature = new ol.Feature({
            geometry:geom
        })
        //創(chuàng)建矢量圖層
        var vectorLayer = LayerUtil.vectorLayer()
        //添加軌跡
        vectorLayer.getSource().addFeature(feature)
        //設(shè)置樣式
        vectorLayer.setStyle(this.getStyle())
        this.map.addLayer(vectorLayer)
        
        //添加起點(diǎn)
        var startFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[0])
        })
        startFeature.setStyle(new ol.style.Style({
            text:new ol.style.Text({
                font:'15px sans-serif',
                text:'起點(diǎn)',
                offsetY:-10
            })
        }))
        vectorLayer.getSource().addFeature(startFeature)
        //添加終點(diǎn)
        var endFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[coordinates.length - 1])
        })
        endFeature.setStyle(new ol.style.Style({
            text:new ol.style.Text({
                font:'15px sans-serif',
                text:'終點(diǎn)',
                offsetY:-10
            })
        }))
        vectorLayer.getSource().addFeature(endFeature)

        //添加小車,這里使用一個(gè)圓點(diǎn)代替
        var carFeature = new ol.Feature({
            geometry:new ol.geom.Point(coordinates[0])
        })
        this.geoMarker = carFeature
        vectorLayer.getSource().addFeature(carFeature)
        this.vectorLayer = vectorLayer
    }

    //設(shè)置速度
    setSpeed(speed){
        this.speed = speed
    }

    //開始播放
    play(){
        if (this.animating) {
            this._stopAnimation(false);
          } else {
            //刪除開始添加的點(diǎn)
            this.vectorLayer.getSource().removeFeature(this.geoMarker)
            this.animating = true//設(shè)置為已經(jīng)開始動畫
            this.now = new Date().getTime()//獲取當(dāng)前時(shí)間
            var self = this
            //監(jiān)聽圖層渲染后的事件
            this.vectorLayer.on('postrender', function(event){
                self._moveFeature(event)
            });
          }
    }

    //停止
    stop(){
        this._stopAnimation(true);
    }

    //移動要素
    _moveFeature(event){
        //獲取渲染圖層的畫布
        var vectorContext = ol.render.getVectorContext(event);
        //獲取當(dāng)前渲染幀狀態(tài)的對象
        var frameState = event.frameState;

        if (this.animating) {
            //渲染時(shí)的時(shí)間減去開始播放軌跡的時(shí)間
            var elapsedTime = frameState.time - this.now;
            var index = Math.round(this.speed * elapsedTime / 1000);
            console.log(index)
            if (index >= this.routeLength) {
                this._stopAnimation(true);
                return;
            }

            //取出坐標(biāo)點(diǎn)重新繪制
            var currentPoint = new ol.geom.Point(this.routeCoords[index]);
            var feature = new ol.Feature(currentPoint);
            vectorContext.drawFeature(feature,this.getStyle());
        }
        this.map.render();
    }

    _stopAnimation(ended){
        this.animating = false;
        var coord = ended ? this.routeCoords[this.routeLength - 1] : this.routeCoords[0];
        var geometry = this.geoMarker.getGeometry();
        geometry.setCoordinates(coord);
        this.vectorLayer.getSource().addFeature(this.geoMarker)
        this.vectorLayer.un('postrender', this._moveFeature);
    }
}

個(gè)人博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末媳搪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子骤宣,更是在濱河造成了極大的恐慌秦爆,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件憔披,死亡現(xiàn)場離奇詭異等限,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)芬膝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門望门,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锰霜,你說我怎么就攤上這事筹误。” “怎么了癣缅?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵厨剪,是天一觀的道長哄酝。 經(jīng)常有香客問我,道長祷膳,這世上最難降的妖魔是什么陶衅? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮直晨,結(jié)果婚禮上搀军,老公的妹妹穿的比我還像新娘。我一直安慰自己勇皇,他們只是感情好罩句,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著儒士,像睡著了一般的止。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上着撩,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天诅福,我揣著相機(jī)與錄音,去河邊找鬼拖叙。 笑死氓润,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的薯鳍。 我是一名探鬼主播咖气,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼挖滤!你這毒婦竟也來了崩溪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤斩松,失蹤者是張志新(化名)和其女友劉穎伶唯,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惧盹,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡乳幸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钧椰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片粹断。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖嫡霞,靈堂內(nèi)的尸體忽然破棺而出瓶埋,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布悬赏,位于F島的核電站狡汉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏闽颇。R本人自食惡果不足惜盾戴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望兵多。 院中可真熱鬧尖啡,春花似錦、人聲如沸剩膘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怠褐。三九已至畏梆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間奈懒,已是汗流浹背奠涌。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留磷杏,地道東北人溜畅。 一個(gè)月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像极祸,于是被迫代替她去往敵國和親慈格。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

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