OpenLayer實現路徑運動

     近期因爲業務的需求,讓我這從未想過要碰Web Gis的業餘前端開發者,走了Web Gis的開發道路。功能需求很簡單,但卻也是讓本身難爲了好幾天。如,應該選擇那個Gis框架,Gis框架的兼容性如何,直接Ie哪些版,能不能簡單到只有一張圖片就行解決問題,等等。。。。。。前端

在如此多的技術盲點,以及不肯定的因素,我開始了征程,現將一些心得作些記錄。web

1、需求分析

     客戶須要的功能就是能在一張Gis圖上實現小車根據路徑進行移動,爲何必定要Gis呢(這是客戶指定需求,無語一該)。而且客戶還說底圖要很容易更換,但他想要用Gis表現的倒是室內的地理信息,我也沒辦法用baidu, 高德等現成的Gis接口。框架

針對上述需求,我沒有去了解過多的web gis框架。由於客戶對Gis的概念就是能放大,縮小,能夠作路徑規劃等。因此我就選擇ol,利用他的靜態圖片(選擇這個是爲知足客戶靈活更新底圖的需求)作Gis底圖的功能來解決此問題。函數

2、效果展現

3、僞代碼實現

因爲是技術驗證代碼, 有些雜亂,現只給出關鍵性代碼。若有業務須要歡迎共同討論。post

3.1 實現路徑的繪製

此步驟仍是相對簡單的,主要用到Ol的Draw對象,代碼哪下:this

draw(type){
        this.stopdraw();
        this._draw = new Draw({
            source: this.layer.getSource(),
            type: type == 'Icon' ? 'Point' : type
        });
        this._draw.on('drawend', (event)=>{
            if(type == 'LineString'){
                this.traceLine = event.feature;
            }
            if(type != 'Icon') return;
            let f = event.feature;
            f.setStyle(new Style({
                image: new Icon({
                    src: '/content/battery.gif'
                }),
                text: new Text({
                    text: 'new item',
                    fill: new Fill({
                        color: "red"
                    })
                })
            }));
            f.type = 'battery';
        });
        this.map.addInteraction(this._draw);
        this._snap = new Snap({source: this.layer.getSource()});
        this.map.addInteraction(this._snap);
    }

關鍵代碼在於drawend事件的監聽,若是是LineString狀況,就將此feature放在一個共公變量,方便路徑運行時使用。spa

 

3.2 分解路徑數據

     此部分就是獲取到3.1步驟的路徑路徑,而後進行解析,由於3.1上的linestring是多個線段的集合,但運動其本質就是改變圖標的座標,使其快速且連續的變化就造成了移動效果。因此這裏有一個方法進行路徑細分,代碼以下:code

cutTrace(){
        let traceCroods = this.traceLine.getGeometry().getCoordinates(); 
        let len = traceCroods.length;
        let destCroods = [];
        for(let i = 0; i < len - 1; ++i){
            let bPoint = traceCroods[i];
            let ePoint = traceCroods[i+1];
            let bevelling = Math.sqrt(Math.pow(ePoint[0] - bPoint[0], 2)
            + Math.pow(ePoint[1] - bPoint[1], 2) );
            let cosA = (ePoint[0] - bPoint[0]) / bevelling;
            let sinA = (ePoint[1] - bPoint[1]) / bevelling;
            
            let curStep = 0;
            let step = 5;
            destCroods.push(new Point([bPoint[0], bPoint[1]]));
            do{
                curStep++;
                let nextPoint;
                if(curStep * step >= bevelling){
                    nextPoint = new Point([ePoint[0], ePoint[1]]);
                }else{
                    nextPoint = new Point([
                        cosA * curStep * step + bPoint[0]
                        ,
                        sinA * curStep * step + bPoint[1]
                    ]);
                }
                destCroods.push(nextPoint);
            }while(curStep * step < bevelling);
        }
        return destCroods;
    }

其中用到了一些數學上的三角函數和計算方法。此方法最終選一個根據步長計算後的座標集合。對象

 

3.3 利用postcompose實現運動效果

代碼以下:blog

tracerun(){
        if(!this.traceLine) return;
        this.traceCroods = this.cutTrace();
        this.now = new Date().getTime();
        this.map.on('postcompose', this.moveFeature.bind(this));
        this.map.render();
    }
    moveFeature(event){
        let vCxt = event.vectorContext;
        let fState = event.frameState;
        let elapsedTime = fState.time - this.now;
        let index = Math.round(300 * elapsedTime / 1000);
        let len = this.traceCroods.length;
        if(index >= len){
            //stop
            this.map.un('postcompose', this.moveFeature);
            return;
        }
        let dx, dy, rotation;
        if(this.traceCroods[index] && this.traceCroods[index + 1]){
            let isRigth = false;
            let bCrood = this.traceCroods[index].getCoordinates();
            let eCrood = this.traceCroods[index + 1].getCoordinates();
            if(bCrood[0] < eCrood[0]){
                //左->右
                isRigth = true
            }
            dx = bCrood[0] - eCrood[0];
            dy = bCrood[1] - eCrood[1];

            rotation = Math.atan2(dy,dx);
            if(rotation > (Math.PI / 2)){
                //修正
                rotation =  Math.PI - rotation;
            }else if(rotation < -1 * (Math.PI / 2)){
                rotation = -1 * Math.PI - rotation;
            }else{
                rotation = -rotation;
            }
            console.log(dx + '  ' + dy + '  ' + rotation);
            let curPoint = this.traceCroods[index];
            var anchor = new Feature(curPoint);
            let style = new Style({
                image: new Icon({
                    img: isRigth ? this.carRight : this.carImg,
                    imgSize: [32,32],
                    rotateWithView: false,
                    rotation: rotation
                }),
                text: new Text({
                    text: 'Car',
                    fill: new Fill({
                        color: 'red'
                    }),
                    offsetY: -20
                })
            });  
            vCxt.drawFeature(anchor, style);
            //this.map.getView().setCenter(bCrood);
        }
        this.map.render();
    }

    此移動代碼的是用ol的postcompose事件進行實現的,由於render方法執行完成後會觸發postcompose事件,因此就代替了定時器的的實現方案。其中rotation根據兩點座標計算出移動圖標的斜度、以及移動的方向等,更爲影響的展現。

相關文章
相關標籤/搜索